Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextViews in a UITableView link detection bug in iOS 7

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.

like image 768
Darren Avatar asked Oct 01 '13 16:10

Darren


2 Answers

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.

like image 148
cbowns Avatar answered Nov 19 '22 07:11

cbowns


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;
  ...
}
like image 19
SuperGuyAbe Avatar answered Nov 19 '22 07:11

SuperGuyAbe