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

时间:2016-07-06 23:46:37

标签: ios objective-c uitableview

问题是当使用强标签类型时: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];
    }

2 个答案:

答案 0 :(得分:2)

代码有一些问题。

1)在设置视图时注册nibs,早在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];
        }
    }
}

答案 1 :(得分:1)

因为dequeueReusableCell函数返回包含旧标签的旧单元格

所以您可以在加载新项目之前删除标签。

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