使用cellForRowAtIndexPath时出现问题

时间:2013-02-06 18:11:42

标签: ios uitableview

我在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];

0 个答案:

没有答案