【问题标题】:NSRangeException in cellForRowAtIndexPathcellForRowAtIndexPath 中的 NSRangeException
【发布时间】:2013-07-19 10:29:55
【问题描述】:

我们正在使用- (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;
}

【问题讨论】:

  • 你会展示你的cellForRowATIndexPath:方法吗?
  • 你没有返回任何单元格.. ;) 这是一个错字,不是你的错误的原因
  • @Jesly Varghese 抱歉,编辑帖子时出错。
  • 请发布完整的错误信息。

标签: ios uitableview


【解决方案1】:

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

【讨论】:

  • UITableView 文档指定 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 将返回一个表示表格单元格的对象,如果单元格不可见或 indexPath 超出范围,则返回 nil。因此,如果指定索引路径处没有单元格,它将返回 nil。但是这里调用 (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 会产生 NSRangeException。
  • 你在numberOfRowsInSection返回什么?
  • 不会导致崩溃/范围异常,优雅地返回nil
  • 崩溃的位置在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多