UICollectionviewCell未在ios 7上更新

时间:2014-09-26 07:08:18

标签: ios uicollectionview uicollectionviewcell

我正在UICollectionViewCell工作,在我的集合视图中,总共有9个单元格,我想在Index' 1'上的CollectioviewCell上添加徽章图标。每次使用buttion动作更新带有int值的Badge。 问题是我第一次通过UIbutton Action更新徽章价值,第一次它工作正常,但第二次更新徽章显示在第8个单元格,再次更新徽章显示在第7个单元格,再次更新显示在第1位置, my Sample Code is here

我的代码中有什么不对请帮帮我.. 在此先感谢

1 个答案:

答案 0 :(得分:0)

尝试这个,我认为它是重复使用单元格,每次重置单元格例如替换打击方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {

   cell = (HomeCollectionView *) [collectionView_ dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
   cell.backgroundColor = [UIColor lightGrayColor];
   cell.CellImageView.image = [UIImage imageNamed:@"Mahasivaratri-Lord-Shiva.jpg"];
   cell.title.text= [NSString stringWithFormat:@"%d",indexPath.row];
   cell.badge.tag = indexPath.row;
   //add this 2 lines
   cell.badge.text = @""; //reset the text
   cell.badge.frame = CGRectMake(cell.bounds.size.width-25,5,25,25); //and also frame of badge
   //
   if (indexPath.row == 1 )
   {
          NSLog(@"unreadMessages---- Menu --- %d  and Indexpath-- %ld",UnreadMessages,(long)indexPath.row);
          NSInteger width  = [self autoBadgeSizeWithString:[NSString stringWithFormat:@"%d",UnreadMessages]];
          cell.badge.frame = CGRectMake(cell.frame.size.width-35, -10,width, 25);
          cell.badge.text = [NSString stringWithFormat:@"%d",UnreadMessages];
          NSLog(@"cell frame --- %@",cell.badge);
   }
   else
   {
      //other code
   }
   return cell;
 }