如何停止自定义单元格包含强标签以覆盖内容?

问题描述:

问题是使用强标签类型时:KILabel可以检测@和#。如何停止自定义单元格包含强标签以覆盖内容?

细胞数10之后它保持电池1和11的值,因此一个2,12

过来写在彼此的文本。

我知道从dequeueReusableCellWithIdentifier问题,但如何解决它的感觉控件的其余部分运行良好,只是这个标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CommentCell *cell; 
    CommentsModels * mycomment = [_CommentsModelsArray objectAtIndex:indexPath.row]; 

    if([mycomment.CommentType integerValue] == 2) 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCellImage"]; 
    }else{ 
     cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"]; 


    } 
    // CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"]; 

    if (!cell) 
    { 


     if([mycomment.CommentType integerValue] == 2) 
     { 
      [ tableView registerNib:[UINib nibWithNibName:@"CommentCellImage" bundle:nil]forCellReuseIdentifier:@"CommentCellImage"]; 


      cell = [ tableView dequeueReusableCellWithIdentifier:@"CommentCellImage"]; 
     }else{ 
      [ tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil]forCellReuseIdentifier:@"CommentCell"]; 


      cell = [ tableView dequeueReusableCellWithIdentifier:@"CommentCell"]; 
     } 




    } 
    cell.commentimage.image = nil; 
    [cell setcell:[_CommentsModelsArray objectAtIndex:indexPath.row]]; 
    cell.commentsViewController = self; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    return cell; 
} 

的setcell功能

- (void) setcell:(CommentsModels*)comment{ 


    User *user = [[HelpManager sharedHelpManager] applicationUser]; 

    UserId = user.userId; 


    _generalcomment = comment; 


    if (_generalcomment.Comment.length > 0) { 
     KILabel *label; 
     label = NULL; 
     label = nil; 
     label = [[KILabel alloc] initWithFrame:CGRectMake(76,66, 180, 14)]; 


       label.taggedUsers = comment.TaggedUsers; 
     NSString *labelText = _generalcomment.Comment; 
     for (TaggedUser *user in comment.TaggedUsers) { 
      NSString *replacedText = [NSString stringWithFormat:@"(@%@)%@",user.UserName,user.FullName]; 
      NSString *tagText = [NSString stringWithFormat:@"@%@",user.UserName]; 
      labelText = [labelText stringByReplacingOccurrencesOfString:tagText withString:replacedText]; 
     } 
     label.text = labelText; 
     label.tag = 1010; 
     label.font = [UIFont systemFontOfSize:12]; 
     label.textColor = [UIColor lightGrayColor]; 
     label.automaticLinkDetectionEnabled = YES; 
     label.linkDetectionTypes = KILinkTypeOptionUserHandle | KILinkTypeOptionHashtag; 
     label.userHandleLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) { 
      TaggedUser *selectedUser = nil; 
      for (TaggedUser *user in comment.TaggedUsers) { 
       if ([string containsString:user.UserName] && [string containsString:user.FullName]) { 
        selectedUser = user ; 
        break; 
       } 
      } 
      if (selectedUser) { 
       ProfileViewController *profileViewController = [STORYBOARD instantiateViewControllerWithIdentifier:@"ProfileViewController"]; 

       profileViewController.ProfileUserId = selectedUser.Id; 

       if (self.commentsViewController != nil) 
       { 
        [self.commentsViewController.navigationController pushViewController:profileViewController animated:YES]; 
       } 
       else{ 
        [_postandCommentsViewController.navigationController pushViewController:profileViewController animated:YES]; 
       } 

      } 
     }; 




     label.hashtagLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) { 
      SearchMasterViewController *searchMasterViewController = [STORYBOARD instantiateViewControllerWithIdentifier:@"SearchMasterViewController"]; 
      searchMasterViewController.searchText = string; 
      if (self.commentsViewController != nil) 
      { 
       [self.commentsViewController.navigationController pushViewController:searchMasterViewController animated:YES]; 
      } 
      else{ 
       [_postandCommentsViewController.navigationController pushViewController:searchMasterViewController animated:YES]; 
      } 

     }; 


     label.urlLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) { 
      // Open URLs 
      [self attemptOpenURL:[NSURL URLWithString:string]]; 
     }; 

     [label adjustFrameSize]; 
     [self.contentView addSubview:label]; 
    } 

因为包含旧标签

这样就可以加载新项目之前删除标签dequeueReusableCell函数返回旧电池。

- (void) setcell:(CommentsModels*)comment { 
    [[self.contentView viewWithTag:1010] removeFromSuperview]; 
    //... your cuttom code here 
} 
+0

@danh是的,但在简单的情况下,如添加或删除标签,它不会产生性能差异。你的回答是好的,我投它:) – larva

该代码有一些问题。

1)注册碎粒,当你设置的观点,早在viewDidLoad中

// in the view controller that is the table's datasource 
// assumes you have an outlet setup in IB to the table view 

@property(weak,nonatomic) IBOutlet UITableView *tableView; 

// ... 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCellImage" bundle:nil]forCellReuseIdentifier:@"CommentCellImage"]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil]forCellReuseIdentifier:@"CommentCell"]; 

    // plus whatever else you do in viewDidLoad 
} 

2)接下来,你可以简化如下现代化您cellForRowAtIndex

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CommentsModels * mycomment = [_CommentsModelsArray objectAtIndex:indexPath.row]; 

    NSInteger type = [mycomment.CommentType intValue]; 
    NSString *identifier = (type == 2)? @"CommentCellImage" : @"CommentCell"; 

    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 

    cell.commentimage.image = nil; 
    [cell setcell:[_CommentsModelsArray objectAtIndex:indexPath.row]]; 
    cell.commentsViewController = self; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    return cell; 
} 

3)最后,setcell:应该将标签创建分解出来,并且只有条件地创建标签(如果单元格中没有一个单元格(在表格第一次显示之后,所有单元格将会))。

// in CommentCell.m 

- (UILabel *)theLabel { 

    KILabel *label = (KILabel *)[cell viewWithTag:1010]; 
    if (!label) { // only create one if its not there 
     label = [[KILabel alloc] initWithFrame:CGRectMake(76,66, 180, 14)]; 
     label.tag = 1010; 
     // everything else you do to create the label goes here, 
     // but NOT anything variable relative to the model, so 
     // for example, not label.text = anything 

     [self.contentView addSubview:label]; 
    } 
    return label; 
} 

现在setcell:稍有理智的,刚开了(可能已经创建)标签,并改变了人们在给定的行仅改变对于给定的模型项目的事情。

- (void) setcell:(CommentsModels*)comment { 
    User *user = [[HelpManager sharedHelpManager] applicationUser]; 
    UserId = user.userId; 
    _generalcomment = comment; 

    if (_generalcomment.Comment.length > 0) { 
     KILabel *label = [self theLabel]; 
     NSString *labelText = _generalcomment.Comment; 
     // I didn't try to understand the following code, but it looks 
     // potentially too slow for configuring a table view cell. 
     // consider doing this calculation just once and caching the result in the model 
     for (TaggedUser *user in comment.TaggedUsers) { 
      NSString *replacedText = [NSString stringWithFormat:@"(@%@)%@",user.UserName,user.FullName]; 
      NSString *tagText = [NSString stringWithFormat:@"@%@",user.UserName]; 
      labelText = [labelText stringByReplacingOccurrencesOfString:tagText withString:replacedText]; 
      [label adjustFrameSize]; 
     } 
    } 
} 
+0

非常感谢真的很感谢 – MOMMH

+0

乐意帮忙。您标记为正确的解决方案虽然代码更快,但却无法解决OP中的一个非常基本的问题。想象一下用户来回滚动你的桌子。相同的标签设置代码会反复运行,每次单元格滚动查看时,您都会删除标签(根据该解决方案),然后重新执行。 – danh