我正在将JSON文件中的图像URL异步加载到UITableView上的图像视图中。图像加载适当,但当我向下滚动订阅源时,用户的个人资料图片会更改为默认头像(当用户尚未加载个人资料照片时使用)。我的php有一个必要的if语句,用于首先检查是否有与该用户相关的配置文件图片。当代码看到没有关联的照片时,它自然会加载默认的化身。
所以。。。当应用程序启动时,我会看到提要和(几秒钟后……也许也有帮助?)提要加载中的图像,但当我滚动经过它们并返回时,它们现在显示为默认化身,并慢慢变回相应的关联图像。我真的不知道从哪里开始。。。这是缓存问题吗?不应该在加载页面之前加载图像吗?非常感谢您对修复我的代码的任何帮助!
我的PHP文件:
$posted="posted";
$query1 = $conn->prepare('SELECT * FROM users WHERE ID=:id');
$query1->bindParam(':id', $_SESSION['uid']);
$query1->execute();
$row = $query1->fetch();
$username="dvdowns";
$zero=0;
$posted="posted";
$query = $conn->prepare('
SELECT description, city, status, state, christmas, country,
needsusername, howmanypeopleneeded, howmanypeoplesignedup,
needs.orgname, needs.ID, titleofneed, expiredate, datesubmitted
FROM needs INNER JOIN follow ON follow.followname = needs.needsusername
WHERE follow.username = :username AND needs.christmas=:zero
AND needs.status=:posted ORDER BY datetime DESC LIMIT 0, 10');
$query->bindParam(':username', $username);
$query->bindParam(':zero', $zero);
$query->bindParam(':posted', $posted);
$query->execute();
$arr = array();
while ($rows = $query->fetch()) {
$title=$rows['titleofneed'];
$description=$rows['description'];
$needsusername=$rows['needsusername'];
$query1 = $conn->prepare('SELECT * FROM users WHERE username=:username');
$query1->bindParam(':username', $needsusername);
$query1->execute();
$row = $query1->fetch();
$photo=$row['photo'];
if ($photo=="") {
$photo = "http://domain.com/images/default.png";
}
else {
$photo="http://www.domain.com/".$photo;
}
$arr['needTitle'] = $title;
$arr['needPoster'] = $needsusername;
$arr['needDescrip'] = $description;
$arr['userImage'] = $photo;
$data[] = $arr;
}
echo json_encode($data);
我的TableViewController.m文件:
NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
NSError *error;
jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
[tableView reloadData]; // if tableView is unidentified make the tableView IBOutlet
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return jsonArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NeedCardTableViewCell *cell = (NeedCardTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"needCard" forIndexPath:indexPath];
NSDictionary *needs = jsonArray[indexPath.row]; // get the data dict for the row
cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *imageURL = [NSURL URLWithString:needs[@"userImage"]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.imageProfPic setImage:image];
});
});
return cell;
使用以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NeedCardTableViewCell *cell = (NeedCardTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[NeedCardTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
NSDictionary *needs = jsonArray[indexPath.row]; // get the data dict for the row
cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];
cell.tag = indexPath.row;
cell.imageView.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:needs[@"userImage"]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
cell.imageView.image = image;
[cell setNeedsLayout];
}
});
}
});
return cell;
}
如果从代码创建单元格,也要确保您已经在viewDidLoad方法中注册了它
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
如果您使用的是故事板单元原型,请跳过这一行。