のこの方法に問題がありますUITableView
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
シミュレーターでは、このメソッドは に新しいセルが表示されたときにのみ呼び出されますUITableView
が、デバイスでは (セルをテーブルに追加した後に) 各セルに対して呼び出されます。テーブル ビューで Web の画像を使用すると、現在のセルが表示されたときにのみ読み込みが開始されます。シミュレーターではすべてが完全に機能しますが、デバイスでは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];