UICollectionView reload data Issue

2019-02-24 08:24发布

I have a problem with data reloading using UICollectionView. I have this array on the viewDidLoad to fill the UICollectionView.

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

and the method:

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

I tap the UIButton and data is reloaded:

[myCollectionView reloadData];

Here how the data looks like before and after the reload: Before

After

4条回答
劫难
2楼-- · 2019-02-24 08:41

It is quit late, but here is my solution.

if you used custom collectionviewcell try to use 'func prepareForReuse()'

查看更多
太酷不给撩
3楼-- · 2019-02-24 08:44
 - (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;
}
查看更多
别忘想泡老子
4楼-- · 2019-02-24 08:59

You add a new label each time you tap the reload button. You should add the label once and change the label text according.

Here a simple example.

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

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

where MyCell will contains a UILabel and a property to modify its text.

I really suggest to take a look at Fun with UICollectionView code by @Ben Scheirman.

Hope that helps.

P.S. Rename myCell to MyCell. A class should start with an uppercase letter.

查看更多
Root(大扎)
5楼-- · 2019-02-24 09:03

You are adding your label when you tap reload button. In that case you are adding label again and again...

So there are three solutions:

  • Make your cell reusable
  • Remove your cell from superview in reload method.
  • You can check if label's text length is not equal to zero. In that case no need to add that label and change text.
查看更多
登录 后发表回答