1

I have a tableview with one UITextView per cell... I have plain text inside this UITextViews, but somehow link parsing (enabled via Interface Builder) seems broken: links are not parsed as links and plain text is clickable and points to the link in the previous cells (check last cell in the screenshot: it's not an URL but it's pointing to the twitter link)...

enter image description here

My code for the tableview population is pretty straightforward...

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ItemCell";
NSString *itemText;

CpCpListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[CpCpListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


if (theTableView == self.searchDisplayController.searchResultsTableView) {
    itemText = [self.filteredArray objectAtIndex:indexPath.row];
} else {
    itemText = [self.items objectAtIndex:indexPath.row];
}


[cell.cellContent setText:nil];
[cell.cellContent setText:itemText];

int len = cell.cellContent.text.length;
NSString *labelValue = [NSString stringWithFormat:@"%@: %d",NSLocalizedString(@"CHARCOUNT", nil),len];
[cell.charCount setText:labelValue];

UITapGestureRecognizer *rowClick = [[UITapGestureRecognizer alloc] initWithTarget:cell action:@selector(handleCellClick)];

//modify this number to recognizer number of tap
[rowClick setNumberOfTapsRequired:1];
[cell.cellContent addGestureRecognizer:rowClick];
[cell.cellContent setSelectable: YES];

cell.root = self;

return cell; 
}

I see these logs in XCode...

 Bad result <Result:HttpURL:{0, 394}>: its range {0, 394} is not completely included in the range {0, 134} of the string it is attached to, Vengono {
    DDResultAttributeName = "<Result:HttpURL:{0, 394}>";
    NSFont = "<UICTFont: 0x155eb2a0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSLink = "http://google.it";
}200 euro{
    DDResultAttributeName = "<Result:Money:{8, 8}>";
    NSFont = "<UICTFont: 0x155eb2a0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSLink = "http://google.it";
} l'uno + 30 di spedizione, 200 se vieni a prenderteli a milano. Sono biglietti prato dx blu x il 29. Ormai introvabili{
    DDResultAttributeName = "<Result:HttpURL:{0, 394}>";
    NSFont = "<UICTFont: 0x155eb2a0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 12.00pt";
    NSLink = "http://google.it";
}
4

1 回答 1

0

只需将UITextView动态添加到每个单元格(并将其从情节提要中删除)即可解决,问题的根本原因是dequeueReusableCellWithIdentifier:CellIdentifier方法,该方法需要强制重置重用单元格的所有子视图。

所以我的代码现在看起来像......

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ItemCell";
    NSString *itemText;
    UITextView *theContent;

    CpCpListCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[CpCpListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    if (theTableView == self.searchDisplayController.searchResultsTableView) {
        itemText = [self.filteredArray objectAtIndex:indexPath.row];
    } else {
        itemText = [self.items objectAtIndex:indexPath.row];
    }


    theContent = [[UITextView alloc] initWithFrame:CGRectMake(10, 15, 300, 107)];    
    theContent.text = itemText;

    NSUInteger len = theContent.text.length;
    NSString *labelValue = [NSString stringWithFormat:@"%@: %lu",NSLocalizedString(@"CHARCOUNT", nil),(unsigned long)len];
    [cell.charCount setText:labelValue];

    // gestures
    UITapGestureRecognizer *rowClick = [[UITapGestureRecognizer alloc] initWithTarget:cell action:@selector(handleCellClick:)];
    [rowClick setNumberOfTapsRequired:1];

    // content properties...
    [theContent addGestureRecognizer:rowClick];
    [theContent setSelectable: YES];
    [theContent setEditable:NO];
    [theContent setDataDetectorTypes:UIDataDetectorTypeAll];

    cell.root = self;
    [cell addSubview:theContent];

    return cell;
}

希望这对有同样问题的人有用。

于 2013-10-26T14:18:31.047 回答