2013-10-25 46 views
1

我有一个tableview与一个UITextView每个单元格...我有这个UITextViews内的纯文本,但不知何故链接解析(通过接口生成器启用)似乎打破:链接不被解析为链接和纯文本是可点击的,并指向在前面的细胞链接(查看最后一个单元格中的截图:它不是一个URL,但它指向Twitter的链接)...UITextView链接解析错误...为什么?

enter image description here

我的实现代码如下人口代码是非常简单的...

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

我看到SE日志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"; 
} 

回答

0

解决仅仅通过增加UITextView dinamically到每一个细胞(从故事板中取出),问题的根源是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; 
} 

希望这可以为人们同样的问题非常有用。

相关问题