我有自定义UITableViewCells包含UITextView.我在Interface Builder中打开了UITextView中的链接检测.当我第一次加载表视图时,一切似乎都在工作,但是当我在表视图中向上和向下滚动时,链接检测会搞砸.具体来说,只有常规文本(通常最初显示)的单元格显示为链接(文本视图中的所有文本都显示为蓝色并且是活动链接),并且链接指向某些文本中的对象.其他表视图单元格.例如,链接可能指向位于不同表视图单元格中的网站,或者将电子邮件发送到位于不同表格视图单元格中的地址.
看起来当表视图单元格被重用时,即使正在更新文本视图文本,链接也会以某种方式被保存.
这只发生在iOS 7,而不是iOS 6.它发生在模拟器和我的设备上.
这是代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sectionKey = [self.orderedSectionKeys objectAtIndex:indexPath.section];
NSDictionary *infoDictionary = [[self.tableViewData objectForKey:sectionKey] objectAtIndex:indexPath.row];
static NSString *cellIdentifier = @"InfoDefaultTableViewCell";
InfoDefaultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"InfoTableViewCells" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.bodyTextView.text = [infoDictionary objectForKey:@"description"];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这里发生了什么,以及如何解决它?
我尝试在设置文本视图文本后添加此代码,尝试重置链接:
cell.bodyTextView.dataDetectorTypes = UIDataDetectorTypeNone;
cell.bodyTextView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
Run Code Online (Sandbox Code Playgroud)
但它并没有改变我所看到的行为.
当用户滚动UITableView过快或者点击状态栏跳转到表格顶部时,会抛出异常.我只是在iPhone 5C设备上看到这次崩溃.我无法在4S,5或32位模拟器上重现它.
使用"All Exceptions"断点,我能够打印掉所有可以通过崩溃递增帧指针的回溯.因为在回溯中没有我的应用程序的痕迹(除了主要),我认为这是一个Apple bug,与之有关[NSRLEArray objectAtIndex:effectiveRange:].这个问题的作者似乎与OS X应用程序有相同的问题,并且评论建议查看NSAttributedString我的应用程序中的用法.我NSAttributedStrings在我的表格视图中使用单元格来链接某些字符串UITextView.但如果这是一个普遍的问题NSAttributedString,这不会导致其他iOS设备崩溃吗?
回溯:
(lldb) bt
* thread #1: tid = 0x2e57bd, 0x3978e6a0 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x3978e6a0 libobjc.A.dylib`objc_exception_throw
frame #1: 0x2f431dc4 CoreFoundation`+[NSException raise:format:] + 112
frame #2: 0x2fd55230 Foundation`-[NSRLEArray objectAtIndex:effectiveRange:] + 120
frame #3: 0x2fd71d76 Foundation`-[NSConcreteMutableAttributedString addAttribute:value:range:] + 198
frame #4: 0x36d7b278 UIFoundation`__50-[NSConcreteTextStorage addAttribute:value:range:]_block_invoke + 156
frame #5: 0x36d7b102 UIFoundation`-[NSConcreteTextStorage addAttribute:value:range:] + 170
frame …Run Code Online (Sandbox Code Playgroud)