2015-04-27 24 views
0

我面临着一个与UITableView单元格有关的问题,它在滚动时会混淆,特别是对于图像。iOS:UITableView单元格在滚动时混合起来

我不知道为什么这是一个。
我已经改变了显示的模式,然后也弄混了。
以下是我的代码。你需要

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"FriendsCell"; 
    FriendsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FriendsTableCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    FriendsTableCell *simpleTableCell = (FriendsTableCell *)cell; 

    _model = [_contentArray objectAtIndex:indexPath.row]; 

    simpleTableCell.nameLabel.text = _model.fullname; 

    if(_friendsSegmentControl.selectedSegmentIndex == 0) { 

     simpleTableCell.followButton.hidden = NO; 
     simpleTableCell.removeButton.hidden = NO; 
     simpleTableCell.unBlockButton.hidden = YES; 
     simpleTableCell.ignoreRequest_btn.hidden = YES; 
     simpleTableCell.rejectRequest_btn.hidden = YES; 
     simpleTableCell.acceptRequest_btn.hidden = YES; 

     simpleTableCell.removeButton.tag = indexPath.row; 
     [simpleTableCell.removeButton addTarget:self action:@selector(deleteFriend_btn:) forControlEvents:UIControlEventTouchUpInside]; 

     simpleTableCell.followButton.tag = indexPath.row; 

     if([_model.isfollowed isEqualToString:@"YES"]) { 
      [simpleTableCell.followButton setImage:[UIImage imageNamed:@"follow_yellow.png"] forState:UIControlStateNormal]; 
      [simpleTableCell.followButton addTarget:self action:@selector(unfollowFriend_btn:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
     else { 
      [simpleTableCell.followButton setImage:[UIImage imageNamed:@"follow_white.png"] forState:UIControlStateNormal]; 
      [simpleTableCell.followButton addTarget:self action:@selector(followFriend_btn:) forControlEvents:UIControlEventTouchUpInside]; 
     } 
    } 

    NSString *escapedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef) _model.prof_pic,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8)); 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^(void) { 

     NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.touristtube.com/%@",escapedString]]]; 
     UIImage *image = [UIImage imageWithData:imageData]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      simpleTableCell.profileImageView.image = image; 
     }); 
    }); 

} 
+0

您正在异步检索图像数据。到检索完成时,单元格已被重新用于不同的行。 – Paulw11

+0

好吧所以似乎是什么解决方案,我应该直接下载图片,请帮助我在这 – Kashif

+0

@ Paulw11,你绝对正确 –

回答

0

两件事情,以确保在对表视图 -

1)关于可重复使用的细胞产生细胞 - 当您正在使用可重复使用的电池,所以在情况下,如果它出队,它会显示数据为它最初创建的索引路径。

它只是在

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

显示它您还可以更新数据,每一个小区之前是正确的,你要更新每个单元(无论新建/出队)与相应的索引路径数据的”容器相应的索引路径为

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

也是如此。

因此,请确保单元格是否被重新使用,您应该使用当前索引路径的数据更新每个容器(单元格子视图)的数据。这样做将解决您的单元格内容重叠问题。

2)关于异步下载图像的细胞 - 兼要异步下载图像,这样就不会阻塞主线程,一旦图像被下载你需要切换到主线程设置图像到单元格。

现在,在异步图像下载的情况下要记住的事情是,您应该始终访问单元格以获取主线程上正确的索引路径,并将下载的图像设置为单元格的图像视图,以获取其调度的索引路径被下载。

所以对于图像,您应该更新您的图像下载方法

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^(void) { 

     NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.touristtube.com/%@",escapedString]]]; 
     UIImage *image = [UIImage imageWithData:imageData]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      FriendsTableCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
      cell.profileImageView.image = image; 
      [cell setNeedsLayout]; 
     }); 
    }); 

这将解决预期要显示的索引路径小区的形象您的问题,但实际上显示了在其他一些指数路径上的细胞。

相关问题