MOMMH MOMMH - 4 months ago 28
iOS Question

How to stop Custom cell contain strong label from overwriting the contents?

the Problem is when using strong label type :KILabel to can detect @ and #.

after the cell number 10 it keep the value of cell 1 and 11 and so one 2 and 12

it over write the text on each other.

I know the problem from dequeueReusableCellWithIdentifier but how can solve it the rest of feel controls working well just this label.

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


the setcell function

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

Answer

because dequeueReusableCell function return old cell that contain old label

so You can remove label before load new item.

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