2015-04-27 29 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]; 
     }); 
    }); 

這將解決預期要顯示的索引路徑小區的形象您的問題,但實際上顯示了在其他一些指數路徑上的細胞。

相關問題