cellForRowAtIndexPath中的NSRangeException

时间:2013-07-19 10:29:56

标签: ios uitableview

我们使用- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法检索特定索引路径的单元格。如果单元格不可见或indexPath超出范围,- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)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;
}

1 个答案:

答案 0 :(得分:0)

您要求的是尚未创建的单元格。例如,它应该在第一个单元格(索引0)上崩溃。一旦表格被填充,你应该要求一个单元格(也就是cellForRowAtIndexPath)。