2015-10-13 217 views
2

我在iOS中创建了一个自定义单元格。自定义单元格中有很多标签。对于少数标签,第一个&第四个自定义单元格的数据总是相同的。我的数据源数组中有5条记录。现在有这些我面临的问题。iOS中创建重复单元格?

  1. 为什么在索引处的行的单元只有在数组中有4条记录时才被调用两次。
  2. 第一行的数据&第四行总是相同的。

请告诉我如何解决此问题。

CODE:

- (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]) 
      { 
       [email protected]"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恢复cell.Because我觉得我得到的时候有超过3行这个问题。

编辑: 我在这里发现了一个奇怪的现象,如果我尝试不使用if(like_count>0) &简单地设置文本FO标签用了这个条件,那么它工作正常,我的条件。

在此先感谢。

+0

更详细地解释一下。像代码或图像等 –

+1

请张贴您的代码,并保留“xcode”标签。 – Raptor

+1

哦..你必须重构'-cellForRowAtIndexPath'很多单一方法的工作,试着练习MVC模式.. – 0yeoj

回答

-1

尝试更换

if (cell==nil) 
{ 
    cell = [[HomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

//if (cell==nil) 
//{ 
    cell = [[HomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
// } 
+0

不适用于我的工作 – TechGuy

0

检查表中元素的个数,并确保该号码是正确的委托numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return the_correct_items_number; 
} 

如果计数正确,请尝试一下cal数组(仅用于测试)。注意:如果您使用网络请求更新源阵列,请考虑使用dispatch_async调用来更新您的内容。

+0

段数返回5这是数组的大小 – TechGuy

+0

好吧,对不起...有时会发生这种情况,但可能不是您的情况 – Lorenzo

+0

您能否通过查看第二个代码告诉我其他东西 – TechGuy

-1

你离队细胞后,写了下面一行: -

[cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
+0

我读了性能它拒绝该单元,所以它可能是问题 – TechGuy

+0

这不会是这样的。对于演出相关问题,应用程序挂起或收到内存警告。你有没有实现我在答案中写的代码? – pkc456

+0

我的意思是iOS重用了一次创建好的单元格,因为当我向下滚动到屏幕时,我得到了重复的单元格。每次滚动** cellForRowAtIndex **时,都会调用 – TechGuy

5

细胞被重用,所以你必须支付执行路径的所有情况。

正如您已经发现的那样,if(like_count>0)if(comment_count>0)意味着如果没有计数,文本标签不会更改,旧值将保留在那里。

您必须添加} else { cell.label_like_count.attributedText = nil }类型的代码来涵盖所有情况。或者,您可以在单元类中的- (void)prepareForReuse中添加一些清理代码(不要忘记[super prepareForReuse])。

+1

使用prepareForReuse是一个清理细胞供下次使用。 –

1

in HomeCell覆盖prepareForReuse:。在该方法中,在所有标签上将.text(或.attributedText)属性设置为nil。然后看看会发生什么。

1

单元格被重用,表格总是只创建当前可见的单元格。当您显示屏幕时,会看到三个单元格。当您滚动并且一个单元格变为隐藏状态并出现另一个单元格时,它实际上是同一个单元格实例。

处理此行为的常用方法是将子类UITableViewCell并在其中进行所有设置。重置为默认状态可以被添加到例如-prepareForReuse方法中。

- (void)prepareForReuse { 
    [super prepareForReuse]; 

    self.first_comment.text = nil; 
    self.second_comment.text = nil; 
    self.third_comment.text = nil; 
} 

要修复您的代码,而无需特定的细胞亚类,首先让我们从你的代码去除重复模式简化:

for (NSUInteger i = 0; i < [user_comments count]; i++) { //i is the traditional variable name for iterating 
    Comment *comment = user_comments[i]; //updating to newer syntax 

    // start of the repetitive pattern 
    NSString *comment_string=[comment.user_name stringByAppendingString:@" "]; 
    comment_string=[comment_string stringByAppendingString:comment.comment]; 
    int count=(int)[comment.user_name length]; 
    NSMutableAttributedString* mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:comment_string]; 

    NSAttributedString 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]; 
    // end of the repetitive pattern 

    if (i == 0) { 
     [cell.first_comment setAttributedText:mutableAttributeStr]; 
    } 
    else if (i == 1) { 
     [cell.second_comment setAttributedText:mutableAttributeStr]; 
    } 
    else if (i == 2) { 
     [cell.third_comment setAttributedText:mutableAttributeStr]; 
    } 
} 

现在你可以重置价值结合起来

NSArray *commentLabels = @[cell.first_comment, cell.second_comment, cell.third_comment]; 

for (NSUInteger i = 0; i < 3; i++) { 
    UILabel *label = commentLabels[i]; 

    // reset if there is no comment 
    if (i >= [user_comments count]) { 
     label.text = nil; 
     continue; 
    } 

    Comment *comment = user_comments[i]; 

    NSString *comment_string = [NSString stringWithFormat:@"%@ %@", comment.user_name, comment.comment]; 
    int count = (int)[comment.user_name length]; 
    NSMutableAttributedString* mutableAttributeStr = [[NSMutableAttributedString alloc]initWithString:comment_string]; 

    NSAttributedString 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]; 

    label.attributedText = mutableAttributeStr; 
} 
+0

如果我让我的代码很短,它会解决问题 – TechGuy

+0

actuallu我不是在运行时创建标签 – TechGuy

+0

@TechGuy我只是把你的代码,简化了它(没有改变功能),然后添加了一个在原代码中丢失的重置。 – Sulthan

0

在您的自定义单元格中使用方法prepareForReuse

//如果单元格是可重用的(具有重用标识符),则在从表视图方法dequeueReusableCellWithIdentifier:返回单元格之前调用该单元格。如果你重写,你必须调用super。

- (void) prepareForReuse { 

// set empty or nil of your repeated element . 

[super prepareForReuse]; 
} 

希望它能帮助你。