我对这种方法有疑问UITableView
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在模拟器中,仅当新单元格出现在 中时才会调用此方法UITableView
,但在设备上会为每个单元格调用(在将单元格添加到表格后)。我在表格视图中使用来自网络的图像,它们仅在当前单元格出现时才开始加载。在模拟器中一切正常,但在设备上图像开始同时加载,因为cellForRowAtIndexPath
每个单元格都被调用。麻烦在哪里?也许我不明白如何cellForRowAtIndexPath
工作?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableCellIdentifier = @"CustomeCell";
CMCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCellIdentifier];
NSMutableDictionary *currentThread = [currentThreadsData objectAtIndex:indexPath.row];
if (!cell)
{
if ([currentThread objectForKey:THREADNAME]) {
cell = [[CMCustomCell alloc] initWithName:YES reuseIdentifier:tableCellIdentifier];
cell.nameLabel.text = [currentThread objectForKey:THREADNAME];
} else {
cell = [[CMCustomCell alloc] initWithName:NO reuseIdentifier:tableCellIdentifier];
}
}
/*HERE IS INITIALIZATION OF CELL`S FIELDS*/
if (![currentThread objectForKey:@"thumbnailimg"]) {
[cell.imageLoadingRoller startAnimating];
CMImageLoader *imgLoader = [[CMImageLoader alloc] initWithDelegate:self];
[imgLoader loadImageFrom:[currentThread objectForKey:THUMBNAIL] tag:indexPath.row];
} else {
[cell.imageLoadingRoller stopAnimating];
cell.thumbnailImageView.image = [currentThread objectForKey:@"thumbnailimg"];
cell.thumbnailImageView.hidden = NO;
}
return cell;
}
这样我在表格中添加新单元格:
for (int i = 0; i < tllAmount; ++i) {
[currentThreadsData addObject:[threads objectAtIndex:i]];
}
[self.loadingRoller stopAnimating];
NSMutableArray *tempData = [[NSMutableArray alloc] init];
for (int i = 0; i < tllAmount; ++i) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[tempData addObject:indexPath];
}
[threadsTable beginUpdates];
[threadsTable insertRowsAtIndexPaths:tempData withRowAnimation:UITableViewRowAnimationTop];
[threadsTable endUpdates];