在下面的代碼中,無論何時滾動tableview,每個單元格中的圖像都在變化,這不應該發生。請幫忙。提前致謝。當tableview被滾動時,單元格中的圖像每次都在變化
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UserDetails *userDetails = [arrUserDetails objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"CustomCell";
__weak TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier];
cell.tag = indexPath.row;
cell.userName.text = userDetails.userName;
[self.operationQueue addOperationWithBlock:^{
NSURL *aURL = [NSURL URLWithString:userDetails.userImageURL];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:aURL options:nil error:&error];
UIImage *image = nil;
if (cell.tag == indexPath.row)
{
image = [UIImage imageWithData:data];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.customImageView.image = image;
cell.customImageView.contentMode = UIViewContentModeScaleToFill;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}];
}
}];
瞭解更多有關如何'UITableView'工作伴侶的圖像的URL。細胞一直出列並重復使用。這就是爲什麼你看到圖像去你認爲是錯誤的單元格,但對於iOS,他們是正確的,因爲'cell.tag == indexPath.row'。您需要比標籤更多的可靠參考。 – NSNoob
使用自定義單元格並定義它的屬性,如名稱標籤和圖像視圖。這將保持堅實的參考,哪個單元是哪個單元並解決由當前代碼引起的問題,即每次調用委託方法時創建UIElements。 – NSNoob
@muku是正確的 - 您不會設置默認圖像,或者至少在加載新圖像時清除舊圖像。另外考慮使用像SDWebImage提供緩存以獲得更好的性能 – Paulw11