Search code examples
iosuitableviewuitextviewios7

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.


Solution

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