UICollectionView reload error

I have a problem reloading data using a UICollectionView . I have this array on viewDidLoad to populate a UICollectionView.

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

and 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 UIButton and the data is reloaded:

 [myCollectionView reloadData]; 

Here's what the data looks like before and after the reboot: Before

After

+4
source share
4 answers

Each time you press the reload button, you add a new label. You must add the label once and change the label text to match.

Here is 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 contain the UILabel and the property to change its text.

I really suggest taking a look at Fun with the UICollectionView code from @Ben Scheirman .

Hope this helps.

PS Rename MyCell to MyCell . The class must begin with an uppercase letter.

+12
source

You add your shortcut when you press the reset button. In this case, you add the shortcut again and again ...

So there are three solutions:

  • Make your cell reusable
  • Remove your cell from the supervisor in reboot mode.
  • You can check if the length of the text label is not equal to zero. In this case, there is no need to add this label and change the text.
+1
source
  - (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; } 
+1
source

It is already late, but here is my solution.

if you used the custom viewviewcell collection try using the func function prepareForReuse () '

0
source

Source: https://habr.com/ru/post/1480429/


All Articles