Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS UICollectionViewPerformance issue
    primarykey
    data
    text
    <p>I am trying to create a simple collection view with custom cells that have an <code>UIImageView</code> as a background. It's going to look something like : </p> <p><img src="https://i.stack.imgur.com/KDKG4.png" alt="enter image description here"></p> <p>This is my code : </p> <pre><code>ViewController.m - (void)viewDidLoad { [super viewDidLoad]; [self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MY_CELL"]; leftInset = rightInset = 0; numberOfDays = 31; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 200; } - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { return numberOfDays; } - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; { MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath]; if(indexPath.row &lt; 10) cell.imageView.image = [UIImage imageNamed:@"no"]; else if(indexPath.section%3==1) { cell.imageView.image = [UIImage imageNamed:@"ok"]; } else if(indexPath.section%3==2) { cell.imageView.image = [UIImage imageNamed:@"sa"]; } else{ cell.imageView.image = [UIImage imageNamed:@"happy_vote"]; } return cell; } #pragma mark – UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(CELL_DIM, CELL_DIM); } - (UIEdgeInsets)collectionView: (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { // top left bottom right return UIEdgeInsetsMake(5, leftInset, 0, 0); } </code></pre> <p>MyCell.m</p> <pre><code>- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSLog(@"Init with frame!"); self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, frame.size.width, frame.size.height)]; [self.contentView addSubview:self.imageView]; [self.imageView setBackgroundColor:[UIColor redColor]]; } return self; } </code></pre> <p>It works fine for around 10 sections in the collection view. The problem is when I want to add more sections, the vertical scrolling starts to move really slow. I noticed that all the cells are allocated all at once. I am searching for a way to make this more resource friendly, and have the scrolling move continuously rather than slow and laggish. Could you tell me how to take care of the resources allocation to avoid this problem ? Thank you.</p> <p>PS : I've tried removing the image view from the cell background and in the method <code>cellForItemAtIndexPath:</code> set a background color for the cell but again, after 10 or more sections it moves really slow.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload