[英]UITextViews in a UITableView link detection bug in iOS 7
我有自定义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;
但它并没有改变我所看到的行为。
这似乎是iOS 7.0的UITextView
的一个错误。 类似的问题有一个似乎有用的解决方法:在将文本视图的文本设置为新文本字符串之前将其设置为nil
。
这里提供的一些建议和通过提供的链接并没有帮助我解决这个问题。
我尝试设置属性文本,将文本设置为nil,将文本设置为@“”。
最后,在一个不可编辑的模式下强制文本视图就可以了。 准备重用
- (void)prepareForReuse
{
...
textView.editable = YES;
textView.editable = NO;
...
}
这些答案都不适用于我(iOS8,Swift),对我来说唯一有用的是首先将文本设置为nil
然后在前面添加一个非visibile空格unicode字符(我选择\
,这是ZERO WIDTH SPACE字符,但任何角色都有效):
textView.text = nil
textView.text = "\u{200B}\(newText)"
找到了解决这个问题的更好方法。 每次设置文本时都需要额外的步骤。 但这肯定解决了这个问题。
_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。
将文本设置为nil
对我来说在一个非常类似的问题中不起作用,但是将scrollEnabled设置为NO
,就像这里建议的那样,对我来说是个窍门。
编辑:此外还有一个非常特殊的情况,导致问题:当一个框开始一个链接并且新文本被设置为空文本(@“” - 不是零!)时,该框以某种方式“破坏”并从那时开始在任何新文本成为一个链接。 我的解决方案是覆盖setText,先将[super text]设置为@“x”,然后再设置为实际的新文本。 将其设置为nil也没有解决这个问题。
由于上述任何解决方案都适用于我,我发现一种解决方法是在您应该更新每个单元格的文本而不是在可重用单元格中重用时创建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];
}
您可以使用NSAttributedString解决此问题。
cell.textview.attributedText = [[NSAttributedString alloc] initWithString:message.message];
似乎在设置新文本之前将文本设置为nil
不起作用。 您可能还需要能够滚动文本视图,因此设置scrollEnabled
可能不是一个选项。
但是,当您在单元格的文本视图中创建和设置属性字符串时,它可以正常工作。 我使用了一个集合视图 ,但它应该与表视图相同。 我在collectionView:cellForItemAtIndexPath:
写了以下几行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;
希望这可以帮助 :)。
没有建议的解决方法适合我。 因此,每次重复使用单元格时,我决定创建一个新的UITextView并将其替换为旧的UITextView。 它不是理想的性能,但至少它可以工作。
这个对我来说可靠:
// fix url detection bug
cell.textView.dataDetectorTypes = UIDataDetectorTypeNone;
cell.textView.dataDetectorTypes = UIDataDetectorTypeLink;
将色调颜色更改为其他颜色实际上有效。 但是,如果可选择启用,则色调也将是相同的颜色。
我也遇到了这个问题,我发现解决这个问题是按以下顺序设置textview属性,
[textview setDataDetectorTypes:UIDataDetectorTypeNone ];
textView.editable = NO;
[textView setDataDetectorTypes:UIDataDetectorTypeLink];
textView.text=@"The text you need to show with link";
希望这能帮到你......
textView.text = nil;
textView.attributedText = nil;
textView.attributedText = [[NSAttributedString alloc] initWithString:@"your new string"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.