本文介绍了UITextViews在iOS 7中的UITableView链接检测错误中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含UITextView的自定义UITableViewCells。我在Interface Builder中打开了UITextView中的链接检测。当我第一次加载表视图时,一切似乎都在工作,但是当我在表视图中向上和向下滚动时,链接检测会搞砸。具体来说,只有常规文本(最初通常呈现)的单元格显示为链接(文本视图中的所有文本都是蓝色并且是活动链接),并且链接指向某些文本中的对象。其他表视图单元格。例如,链接可能指向位于不同表视图单元格中的网站,或者将电子邮件发送到位于不同表格视图单元格中的地址。

I have custom UITableViewCells that contain a UITextView. I have link detection in the UITextView turned on in Interface Builder. When I first load the table view, everything seems to be working, but as I scroll up and down the table view, the link detection gets messed up. Specifically, cells that just have regular text (which are presented normally initially) are being shown as links (all the text in the text view is coloured blue and is an active link), and the links point to objects that are in some of the other table view cells. For example a link might point to a website that was in a different table view cell, or launch an email to an address that was in a different table view cell.

It看起来当表视图单元格被重用时,即使正在更新文本视图文本,链接也会以某种方式被保存。

It seems like when the table view cells are being reused, even though the text view text is being updated, the links are somehow getting saved.

这只发生在iOS 7中,不是iOS 6.它发生在模拟器和我的设备上。

This only happens in iOS 7, not iOS 6. It happens in the simulator and on my device.

这是代码:

- (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;
}

有谁知道这里发生了什么,以及如何解决?

Does anyone know what is happening here, and how to solve it?

我在设置文本视图文本后尝试添加此代码,尝试重置链接:

I tried adding this code after setting the text view text, to try to reset the links:

cell.bodyTextView.dataDetectorTypes = UIDataDetectorTypeNone;
cell.bodyTextView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;

但它没有改变我所看到的行为。

but it didn't change the behaviour that I'm seeing.

推荐答案

这似乎是iOS 7.0的 UITextView 中的错误。 有一个似乎有用的解决方法:将文本视图的文本设置为 nil

This appears to be a bug in iOS 7.0's UITextViews. A similar question has a workaround which seems to help: set the text view's text to nil before setting it to the new text string.

这篇关于UITextViews在iOS 7中的UITableView链接检测错误中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:16