2

我们正在使用- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法来检索特定索引路径处的单元格。- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath如果单元格不可见或 indexPath 超出范围,将返回表示表格单元格的对象或 nil。但是重复调用会- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath导致 NSRange 异常。可能是什么原因?

NSIndexPath *ip = [NSIndexPath indexPathForRow:index inSection:0];
        CustomCell *cell = (CustomCell *)[tableview_ cellForRowAtIndexPath:ip];

代码

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = nil;
static NSString *cellIdentifier = @"Cell";
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil];
    cell = [nibs objectAtIndex:0];
}
ImageCache *imageCache = [[ImageCache alloc]initWithUrl:sentCard.sentImage];
    UIImage *cachedImage = [imageCache fetchImageFromCache];
    if (cachedImage!=nil) {
        cell.cardImage.image = [Utility scaleImageToSize:cell.cardImage.frame.size image:cachedImage];
        [cell.loadingIndicator stopAnimating];
    }
    else {
        imageCache.cacheDelegate = self;
        imageCache.cacheDuration = 24.0;
        [imageCache fetchImage];
    }
return cell;
}
4

1 回答 1

0

您正在请求一个尚未创建的单元格。例如,这应该在第一个单元格(索引 0)上崩溃。cellForRowAtIndexPath一旦表格被填充,您应该要求一个单元格(又名)。

于 2013-07-19T10:35:39.223 回答