UITableView 拉取每个用户的特定请求
Posted
技术标签:
【中文标题】UITableView 拉取每个用户的特定请求【英文标题】:UITableView Pull Specific Request Per User 【发布时间】:2013-12-22 20:00:34 【问题描述】:我不确定如何尝试为每个应用用户提取特定请求,例如每次为每个应用用户显示单独的朋友列表。我在 NSURL 中尝试了 POST/GET 请求,但没有运气。如何在加载时指定用户参数以根据登录用户加载不同的表?目前,表格视图中没有返回任何内容。如果您需要更多信息,请告诉我。
NSURL *url = [NSURL URLWithString:@"https://***/friendsList.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
NSURLConnection *conneection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[request setHTTPMethod:@"GET"];
NSString *postString = savedUser;
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[[NSURLConnection alloc] initWithRequest:request delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
data = [[NSMutableData alloc]init];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
[data appendData:theData];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.friendsList = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error: nil];
[friendsListTable reloadData];
PHP:
<?php
header('Content-type: application/json');
$username = 's***';
$password = '***';
$host = 'localhost';
$dbname = '***';
$link = mysql_connect('localhost', $username, $password);
if (!$link)
die('Could not connect: ' . mysql_error());
else
$appUser = $_POST['username'];
// make foo the current db
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected)
die ('Can\'t login to shipstudent : ' . mysql_error());
$get_news = mysql_query("Select username, first_name, last_name from Users u inner join Friends f on f.friendid = u.iduser where u.username = '$appUser'", $link);
$articles = array();
while($row = mysql_fetch_assoc($get_news))
$articles [] = $row;
echo json_encode($articles);
?>
【问题讨论】:
【参考方案1】:我是这样做的:
NSString *userParam = [NSString stringWithFormat:@"https://***.php?username=%@",savedUser];
NSURL *url = [NSURL URLWithString:userParam];
【讨论】:
以上是关于UITableView 拉取每个用户的特定请求的主要内容,如果未能解决你的问题,请参考以下文章
文档/库实现拉取以在 UICollectionView/ UITableView 中显示更多菜单选项