我在iOS中创建了一个自定义单元格。在自定义单元格中有很多标签。对于少数标签,第一个和第四个自定义单元格的数据始终相同。我的数据源数组中总共有5条记录。现在有我面临的这些问题。
- 当数组中有 4 条记录时,为什么索引处的行单元格仅被调用两次。
- 第一行和第四行的数据始终相同。
请告诉我如何解决这个问题。
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cell for row called %d",(int)[arr_post count]);
//define variables here
NSMutableAttributedString *mutableAttributeStr;
NSAttributedString *attributeStr;
static NSString *CellIdentifier = @"homeCell";
float x_pos;
HomeCell *cell = [self.table_view
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//get the post data
Post *user_post=[arr_post objectAtIndex:indexPath.row];
cell.tv_post.text=user_post.post_description;
cell.tv_post.font = [UIFont fontWithName:user_post.font_family size:[user_post.font_size floatValue]];
cell.label_name.text=user_post.post_title;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:user_post.modification_date];
if([user_post.post_image isEqualToString:@"none"] && [user_post.post_video isEqualToString:@"none"])
{
x_pos=cell.tv_post.frame.origin.x;
cell.tv_post_leading_space.constant=-(x_pos);
[cell.img_post setHidden:true];
}
//set the like count
NSString *first_like_user=user_post.recent_like_name;
int count=(int)[first_like_user length];
float like_count=[user_post.like_count intValue];
//chek if tehre are any likes on the post
NSLog(@"recent like name is %@",user_post.recent_like_name);
NSLog(@"like count is %f",like_count);
if(like_count>0)
{
NSLog(@"inside like count block");
NSString *str_like_count=[NSString stringWithFormat:@"%lu",(unsigned long)like_count-1];
if(like_count==1)
{
if([myUsername isEqualToString:first_like_user])
{
first_like_user=@"You like this post";
count=3;
}
else
{
first_like_user=[first_like_user stringByAppendingString:@" like this post"];
}
}
else if(like_count==2)
{
first_like_user=[first_like_user stringByAppendingString:@" and "];
str_like_count=[str_like_count stringByAppendingString:@" other like this post"];
first_like_user=[first_like_user stringByAppendingString:str_like_count];
}
else
{
if(like_count>1000)
{
like_count=like_count/1000;
str_like_count=[NSString stringWithFormat:@"%lu",(unsigned long)like_count];
str_like_count=[str_like_count stringByAppendingString:@"k"];
first_like_user=[first_like_user stringByAppendingString:@" and "];
str_like_count=[str_like_count stringByAppendingString:@" others like this post"];
first_like_user=[first_like_user stringByAppendingString:str_like_count];
}
else
{
first_like_user=[first_like_user stringByAppendingString:@" and "];
str_like_count=[str_like_count stringByAppendingString:@" others like this post"];
first_like_user=[first_like_user stringByAppendingString:str_like_count];
}
}
mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:first_like_user];
attributeStr = [[NSAttributedString alloc]initWithString:@"\n" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[mutableAttributeStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:14.0] range:NSMakeRange(0, count)];
[mutableAttributeStr addAttribute:NSForegroundColorAttributeName value:[self colorFromHexString:@"#48a0dd"] range:NSMakeRange(0, count)];
[mutableAttributeStr appendAttributedString:attributeStr];
//set the like label dynamic height & width
cell.label_like_count.attributedText = mutableAttributeStr;
CGSize maximumLabelSize = CGSizeMake(187,9999);
CGSize requiredSize = [cell.label_like_count sizeThatFits:maximumLabelSize];
CGRect labelFrame = cell.label_like_count.frame;
labelFrame.size.height = requiredSize.height;
cell.label_like_count.frame = labelFrame;
// cell.label_like_count.lineBreakMode = NSLineBreakByWordWrapping;
cell.label_like_count.numberOfLines = 0;
[cell.label_like_count sizeToFit];
[cell.label_like_count setAttributedText:mutableAttributeStr];
}
//show dynamic comment
NSMutableArray *user_comments=user_post.comments;
float comment_count=[user_post.comment_count intValue];
NSLog(@"arr comments count is %lu",(unsigned long)comment_count);
if(comment_count>0)
{
NSLog(@"post id is %@",user_post.id);
NSMutableAttributedString *mutableAttributeStr;
NSAttributedString *attributeStr;
for(l=0;l<[user_comments count];l++)
{
NSLog(@"inside loop %d",l);
Comment *comment=[user_comments objectAtIndex:l];
if(l==0)
{
NSLog(@"l is zero");
NSString *comment_string=[comment.user_name stringByAppendingString:@" "];
comment_string=[comment_string stringByAppendingString:comment.comment];
int count=(int)[comment.user_name length];
mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:comment_string];
NSLog(@"comment string is %@",comment_string);
attributeStr = [[NSAttributedString alloc]initWithString:@"\n" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[mutableAttributeStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:14.0] range:NSMakeRange(0, count)];
[mutableAttributeStr addAttribute:NSForegroundColorAttributeName value:[self colorFromHexString:@"#48a0dd"] range:NSMakeRange(0, count)];
[mutableAttributeStr appendAttributedString:attributeStr];
[cell.first_comment setAttributedText:mutableAttributeStr];
}
else if(l==1)
{
NSLog(@"l is 1");
NSString *comment_string=[comment.user_name stringByAppendingString:@" "];
comment_string=[comment_string stringByAppendingString:comment.comment];
int count=(int)[comment.user_name length];
mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:comment_string];
attributeStr = [[NSAttributedString alloc]initWithString:@"\n" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[mutableAttributeStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:14.0] range:NSMakeRange(0, count)];
[mutableAttributeStr addAttribute:NSForegroundColorAttributeName value:[self colorFromHexString:@"#48a0dd"] range:NSMakeRange(0, count)];
NSLog(@"comment string is %@",comment_string);
[mutableAttributeStr appendAttributedString:attributeStr];
[cell.second_cmment setAttributedText:mutableAttributeStr];
}
else if(l==2)
{
NSLog(@"l is 2");
NSString *comment_string=[comment.user_name stringByAppendingString:@" "];
comment_string=[comment_string stringByAppendingString:comment.comment];
int count=(int)[comment.user_name length];
mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:comment_string];
attributeStr = [[NSAttributedString alloc]initWithString:@"\n" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[mutableAttributeStr addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"Helvetica-Bold" size:14.0] range:NSMakeRange(0, count)];
[mutableAttributeStr addAttribute:NSForegroundColorAttributeName value:[self colorFromHexString:@"#48a0dd"] range:NSMakeRange(0, count)];
[mutableAttributeStr appendAttributedString:attributeStr];
[cell.third_comment setAttributedText:mutableAttributeStr];
}
}
}
else
{
NSLog(@"not inside loop");
}
cell.label_time.text=[BaseController getTimestampForDate:date];
return cell;
}
编辑: 我在某处读到,出于性能原因,UITable 恢复了单元格。因为我觉得当超过 3 行时我会遇到这个问题。
编辑:
我在这里发现了一个奇怪的情况,如果我尝试不使用条件 if(like_count>0)
并简单地设置文本 fo 标签而不使用此条件,那么它对我来说效果很好。
提前致谢。
最佳答案
单元格被重用,因此您必须覆盖执行路径中的所有情况。
正如您已经发现的,if(like_count>0)
和 if(comment_count>0)
意味着如果没有计数,则文本标签不会更改,并且旧值将保留在那里。
您必须添加 } else { cell.label_like_count.attributedText = nil }
类型的代码来涵盖所有情况。或者,您可以在单元类的 - (void)prepareForReuse
中添加一些清理代码(不要忘记 [super prepareForReuse]
)。
关于ios - iOS 中创建了重复的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33100095/