[英]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.