2013-05-13 53 views
4

我使用UICollectionView重新载入数据时遇到问题。我对viewDidLoad这个数组填补了UICollectionView.UICollectionView重新载入数据问题

array = [[NSMutableArray alloc] init]; 
[array addObject:@"1"]; 
[array addObject:@"2"]; 
[array addObject:@"3"]; 
[array addObject:@"4"]; 
[array addObject:@"5"]; 
[array addObject:@"6"]; 

和方法:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"Cell"; 
    //cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    myCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 100, 20)]; 
    [titleLabel setText:[array objectAtIndex:indexPath.row]]; 
    titleLabel.textColor = [UIColor whiteColor]; 
    titleLabel.backgroundColor = [UIColor clearColor]; 
    [cell addSubview:titleLabel]; 

    return cell; 
} 

我挖掘UIButton和数据重新加载:

[myCollectionView reloadData]; 

这里的数据如何外观就像重装前后一样: Before

After

回答

12

您每次点击重新加载按钮时都会添加一个新标签。您应该添加标签一次并根据标签文本更改。

这里有个简单的例子。

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

    MyCell *cell = (MyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" 
                       forIndexPath:indexPath]; 
    [cell setMyTextLabel:indexPath.row]; 
    return cell; 
} 

其中MyCell遗嘱载有UILabel和属性修改其文本。

我真的建议看看Fun with UICollectionView代码@Ben Scheirman

希望有所帮助。

P.S.将myCell重命名为MyCell。一个类应该以大写字母开头。

+0

请告诉我什么是“MyCell”,是否为 – Chandru 2014-10-01 07:13:52

+0

还有什么更好的办法?你必须创建自定义单元格,以避免在uicollectionview中缓存数据 – TomSawyer 2014-10-24 13:54:14

1

当您点击重新加载按钮时,您正在添加您的标签。在这种情况下,你一次又一次地将标签...

因此,有三种解决方案:

  • 让你的电池可重复使用的
  • 从上海华盈在重载方法删除你的细胞。
  • 您可以检查标签的text长度是否不等于零。在这种情况下,不需要添加该标签并更改文本。
+0

但如何从重装的上海华除去细胞? – ThePunisher 2013-05-13 08:50:49

1
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
     UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 
dispatch_async(dispatch_get_main_queue(), ^{ 

    // ********* Changed ******* 

     for (UIView *v in [cell.contentView subviews]) 
     [v removeFromSuperview]; 

// ********** Changed ********** 
      if ([self.collectionviewFlow.indexPathsForVisibleItems containsObject:indexPath]) { 
      NSString *img_name=[NSString stringWithFormat:@"%@_thumb%d.png",self.VaritiesName,(int)indexPath.row+1]; 
      imageVw=[[UIImageView alloc]initWithImage:[UIImage imageNamed: img_name]]; 
      imageVw.frame=CGRectMake(10,10,100,100); 
      [cell.contentView addSubview:imageVw]; 
      } 
     }); 
    cell.backgroundColor=[UIColor clearColor]; 
    return cell; 
} 
+0

@pradhyuman sinh:嗨,我也面临着与100个单元格相同的问题。我试过你的解决方案,但它不适用于我的iOS 8。请告诉我解决方案停止重叠关闭单元格问题。 – chaaruu 2014-12-18 12:41:10

0

这是退出晚了,但这里是我的解决方案。

,如果你自定义的collectionviewcell使用 尝试“FUNC prepareForReuse()”使用