【发布时间】:2013-11-24 12:41:16
【问题描述】:
我在UITableView 中设置了一个自定义的UITableView 单元格,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"CELL_IDENTIFIER";
SGCustomCell *cell = (SGCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) cell = [[SGCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell = [self customizedCell:cell withPost:[postsArray objectAtIndex:indexPath.row]];
return cell;
}
我像这样设置单元格(特别是将UITextView.text 设置为nil - 如this answer 中所述):
descriptionLabel.text = nil;
descriptionLabel.text = post.postDescription;
descriptionLabel.frame = CGRectMake(leftMargin - 4, currentTitleLabel.frame.origin.y + currentTitleLabel.frame.size.height + 10, self.frame.size.width - topMargin * 3, 100);
[descriptionLabel sizeToFit];
单元格是 100% 可重复使用的,UITextView 是这样初始化的(如您所见,没什么特别的):
descriptionLabel = [[UITextView alloc] init];
descriptionLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:11];
descriptionLabel.editable = NO;
descriptionLabel.scrollEnabled = NO;
descriptionLabel.dataDetectorTypes = UIDataDetectorTypeLink;
descriptionLabel.frame = CGRectMake(leftMargin, currentTitleLabel.frame.origin.y + currentTitleLabel.frame.size.height + 10, self.frame.size.width - topMargin * 3, 10);
[self addSubview:descriptionLabel];
但是当表格有大约 50 个单元格并且当我 快速滚动它时,我会遇到以下崩溃:
Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
这绝对是荒谬的 - 我注释掉了这一行 - descriptionLabel.dataDetectorTypes = UIDataDetectorTypeLink; 并且应用程序停止崩溃!我花了几个小时试图找出问题所在,现在我明白了。
在 iOS 7.0.3 上测试
【问题讨论】:
-
你确定不是
[postsArray objectAtIndex:indexPath.row]?? -
当然不是!仅当数据检测器类型打开时才会发生崩溃。
标签: ios cocoa-touch uitableview uitextview