首页 文章

UITextViews在iOS 7中的UITableView链接检测错误中

提问于
浏览
46

我有自定义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;
}

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


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

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

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

13 回答

  • 38

    这似乎是iOS 7.0的 UITextView 中的一个错误 . similar question有一个似乎有用的解决方法:在将文本视图的文本设置为新文本字符串之前将其设置为 nil .

  • 5

    这里提供的一些建议和通过提供的链接并没有帮助我解决这个问题 .

    我尝试设置属性文本,将文本设置为nil,将文本设置为@“” .

    最后,在一个不可编辑的模式下强制文本视图就可以了 . 准备重用

    - (void)prepareForReuse
    {
      ...
        textView.editable = YES;
        textView.editable = NO;
      ...
    }
    
  • 1

    这些答案都不适用于我(iOS8,Swift),对我来说唯一有用的是首先将文本设置为 nil 然后在前面添加一个非可见的空格unicode字符(我选择 \u200B ,这是ZERO WIDTH SPACE字符,但任何角色都有效):

    textView.text = nil
    textView.text = "​\u{200B}\(newText)"
    
  • 16

    找到了解决这个问题的更好方法 . 每次设置文本时都需要额外的步骤 . 但这肯定解决了这个问题 .

    _textView.selectable = NO; // set it to NO clears all possible data detection work so far.
    _textView.selectable = YES; // set it to YES so that actual links are detected.
    

    基本上数据检测需要将 selectable 字段设置为YES才能工作 . 当您将其设置为NO时,将其完全删除 .

    注意:这仅适用于ios7 .

  • 3

    将文本设置为 nil 对于我来说在一个非常类似的问题中不起作用,但将scrollEnabled设置为 NO ,就像建议的here一样,对我来说是个窍门 .

    编辑:此外还有一个非常特殊的情况,导致问题:当一个框开始一个链接并且新文本被设置为空文本(@“” - 不是零!)时,该框以某种方式“破坏”并从那时开始在任何新文本成为一个链接 . 我的解决方案是覆盖setText,先将[super text]设置为@“x”,然后再设置为实际的新文本 . 将其设置为nil也没有解决这个问题 .

  • -1

    由于以上任何一种解决方案都适用于我,我发现一种解决方法是在您应该更新每个单元格的文本而不是在可重用单元格中重用时创建 UITextView .

    您在 UITableViewCellDataSource 中的代码将是:

    - (void)updateDescriptionWithText:(NSString *)descriptionText
    {
        UITextView *descriptionTV = [[UITextView alloc] initWithFrame:aFrame];
        [descriptionTV setScrollEnabled:NO]; 
        [descriptionTV setEditable:NO]; 
        [descriptionTV setDataDetectorTypes:UIDataDetectorTypeLink]; 
        if ([descriptionV respondsToSelector:@selector(setSelectable:)]) 
         [descriptionTV  setSelectable:YES];
        [descriptionTV setText:descriptionText];
        [self addSubview:descriptionTV];
    }
    

    并在 UITableViewCell

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       ***your init code***
    
       [cell updateDescriptionWithText:description];
    }
    
  • 2

    您可以使用NSAttributedString解决此问题 .

    cell.textview.attributedText = [[NSAttributedString alloc] initWithString:message.message];
    
  • 0

    似乎在设置新文本之前将文本设置为 nil 不起作用 . 您可能还需要能够滚动文本视图,因此可能无法设置 scrollEnabled .

    但是,当您创建 attributed string 到单元格的文本视图时,它可以工作 . 我使用了一个集合视图,但它应该与表视图相同 . 我在 collectionView:cellForItemAtIndexPath: 写了以下几行

    //get the color and other properties from your UITextView
    UIColor *textColor = cell.textView.textColor;
    UIFont *messageFont = cell.textView.font;
    
    //create your attributed string
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:yourStringForTheTextView attributes:@{NSForegroundColorAttributeName:textColor, NSFontAttributeName:messageFont}];
    
    //set it to your UITextView
    cell.textView.attributedText = attributedString;
    

    希望这可以帮助 :) .

  • 2

    没有建议的解决方法适合我 . 因此,每次重复使用单元格时,我决定创建一个新的UITextView并将其替换为旧的UITextView . 它不是理想的性能,但至少它可以工作 .

  • 0

    这个对我来说可靠:

    // fix url detection bug
    cell.textView.dataDetectorTypes = UIDataDetectorTypeNone;
    cell.textView.dataDetectorTypes = UIDataDetectorTypeLink;
    
  • 1

    将色调颜色更改为其他颜色实际上有效 . 但是,如果可选择启用,则色调也将是相同的颜色 .

  • 6

    我也遇到了这个问题,我发现解决这个问题是按以下顺序设置textview属性,

    [textview setDataDetectorTypes:UIDataDetectorTypeNone ];
    textView.editable = NO;
    [textView setDataDetectorTypes:UIDataDetectorTypeLink];
    textView.text=@"The text you need to show with link";
    

    希望这能帮到你......

  • 19
    textView.text = nil;
    textView.attributedText = nil;
    
    textView.attributedText = [[NSAttributedString alloc] initWithString:@"your new string"];
    

相关问题