46

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 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.

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

Here is the code:

- (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.

Darren
  • 10,091
  • 18
  • 65
  • 108
  • Try printing out the text being saved into `bodyTextView`: it'll help show you if `UITableViewCell` is caching its rendered contents or if the string being retrieved isn't what you expected. – cbowns Oct 01 '13 at 19:26
  • I can log the text of the bodyTextView after I set it. It logs what I expect (the text that I set it to). – Darren Oct 01 '13 at 23:28
  • Interesting. What happens when you call `setNeedsDisplay` on a cell that's showing the wrong string in the UI? (You can do this in the debugger to make it easy to try.) – cbowns Oct 01 '13 at 23:34
  • The correct string shows in all the table view cells. The problem is that normal strings show as links. So for example I could have the text: "This is my text." and it will be in a blue font colour and link to, for example, a website that is written in the text view of another table view cell. – Darren Oct 01 '13 at 23:43
  • Oh, I see. Sorry, I misread earlier. It sounds like a UIKit bug. Have you tried manually setting/resetting properties surrounding link detection properties on the text view after setting the new text? (in an effort to have it re-parse and render its contents) – cbowns Oct 01 '13 at 23:54
  • Yes I did try that, no luck. I'll add what I did to my question. I think that it is a UIKit bug, I hope I can find a way to get around it though until it's fixed. – Darren Oct 02 '13 at 00:05
  • 1
    Gotcha. I just found this answer on another related thread which sounds helpful? http://stackoverflow.com/a/18968687/774 – cbowns Oct 02 '13 at 00:09
  • Excellent, it worked! I had seen that question, but I hadn't seen that answer. Thanks!! – Darren Oct 02 '13 at 00:11
  • Use AttributedStrings for your textView. See [this StackOverflow answer][1]. [1]: http://stackoverflow.com/a/20669356/340354 – scrrr Mar 06 '14 at 13:52
  • I've answered this question here. http://stackoverflow.com/a/22457773/821690 – mrmichaeldev Mar 18 '14 at 16:45

13 Answers13

37

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.

Community
  • 1
  • 1
cbowns
  • 6,295
  • 5
  • 47
  • 64
19

Several suggestions here and through links provided did not help me with this bug.

I tried setting attributed text, setting text to nil, setting text to @"".

In the end forcing the text view in an out of editable mode did the trick. In prepare for reuse

- (void)prepareForReuse
{
  ...
    textView.editable = YES;
    textView.editable = NO;
  ...
}
SuperGuyAbe
  • 525
  • 1
  • 5
  • 5
  • This works. Found this was resetting textColor instead of improperly adding link, so had to reset textColor too. Unfortunately Instruments is telling me that all these steps combined are even slower than using `setAttributedText:`, on iPod Touch iOS 7.1. – TalkLittle May 12 '14 at 19:11
  • This worked for me, but only when I also set the attributedText to nil before updating the text property. – nacross Dec 19 '14 at 00:46
16

None of these answers worked for me (iOS8, Swift), the only thing that worked for me was to first set the text to nil and then prepend the new text with a non-visibile whitespace unicode character (I chose \u200B, which is the ZERO WIDTH SPACE character but any character works):

textView.text = nil
textView.text = "​\u{200B}\(newText)"
melvinmt
  • 161
  • 2
  • 3
6

Found a better way to solve this problem. This requires an extra step every single time you set text. But this definitely fixes the problem.

_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.

Basically data detection requires the selectable field to be set to YES to work. When you set it to NO, its completely removed.

Note: This is only for ios7.

Legolas
  • 12,145
  • 12
  • 79
  • 132
5

Setting the text to nil did not work for me in a very similar problem, but setting scrollEnabled to NO, like suggested here, did the trick for me.

Edit: In addition there was still a very special case, that caused problems: When a box began with a link and the new text was set to empty text (@"" - not nil!) the box somehow "broke" and from then on any new text became a link. My solution was to override setText to set [super text] to @"x" first and then to the actual new text. Setting it to nil instead did not solve this problem either.

Community
  • 1
  • 1
Tharagon
  • 117
  • 4
3

As any of the above solutions worked for me, a workaround I've found is to create your UITextView when you are supposed to update the text for each cell instead of reusing in the reusable cell.

Your code in the UITableViewCellDataSource would be :

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

And in the UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ***your init code***

   [cell updateDescriptionWithText:description];
}   
  • you must manage removing `descriptionTV` (if you are reusing your table view cell, and you should reuse your table view cell). – rptwsthi Aug 10 '15 at 14:07
2

You can fix this issue by using NSAttributedString.

cell.textview.attributedText = [[NSAttributedString alloc] initWithString:message.message];
Ardra Thambi
  • 137
  • 2
  • 9
  • this seems to be a proper fix. When you enable data detection, they get separate attributes from the plain text, thus the whole text become NSAttributedString. – Michał Zygar Jan 07 '15 at 11:20
2

It seems that setting the text to nil before setting the new text doesn't work. You also might need to be able to scroll the text view, so setting the scrollEnabled might not be an option.

However, it works when you create and set an attributed string to the cell's text view. I used a collection view, but it should be the same with table views. I wrote the following lines in the 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;

Hope this helps :).

Cornel
  • 581
  • 5
  • 8
  • This is the only solution that works (besides the solutions involving creating a new UITextView each time, which I don't want to do for performance reasons) – Ethan Aug 03 '14 at 20:38
  • This is causing a strange blink in the text – jose920405 Jun 03 '15 at 15:47
1

Non of the suggested workarounds work for me. So I decided to create a new UITextView and replace it with the old one, every time the cell is reused. It is not ideal for performance, but at least it works.

tbergmen
  • 66
  • 2
1

This one works reliably for me:

// fix url detection bug
cell.textView.dataDetectorTypes = UIDataDetectorTypeNone;
cell.textView.dataDetectorTypes = UIDataDetectorTypeLink;
Andrew Bennett
  • 910
  • 11
  • 11
0

Changing the Tint color to other color actually works. However if selectable enable the tint will also be the same color.

Desmond
  • 5,001
  • 14
  • 56
  • 115
0

I also face this problem and I found out to solve this is to set textview properties in the following order,

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

Hope this help you.....

-1
textView.text = nil;
textView.attributedText = nil;

textView.attributedText = [[NSAttributedString alloc] initWithString:@"your new string"];
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63