user3680475 user3680475 - 1 year ago 101
Objective-C Question

Performance in UICollectionView

I'm using a standard approach for a UICollectionView with a custom Cell. I've registered the Cell and all of that, but when the collectionview starts to fill the entire screen with items - the performance drops a lot and it becomes laggy. No problems when there are just a few items. The collection view is dynamic so that users can add items as desired. Also remove. What can be the problem? Or solution to turn it a bit around.

Thanks for assistance!



- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
[_projectsCollectionView registerNib:[UINib nibWithNibName:@"Cell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];

// Configure the style of the three big buttons
prepareButton.layer.cornerRadius = 7;
prepareButton.layer.borderWidth = 1;
prepareButton.layer.borderColor = [UIColor blueColor].CGColor;

aboutButton.layer.cornerRadius = 7;
aboutButton.layer.borderWidth = 1;
aboutButton.layer.borderColor = [UIColor blueColor].CGColor;

createButton.layer.cornerRadius = 7;
createButton.layer.borderWidth = 1;
createButton.layer.borderColor = [UIColor blueColor].CGColor;

editButton.layer.cornerRadius = 7;
editButton.layer.borderWidth = 1;
editButton.layer.borderColor = [UIColor blueColor].CGColor;
// (note - may prefer to use the tintcolor of the control)

// Set up projectsCollectionView
_projectsCollectionView.delegate = self;
_projectsCollectionView.dataSource = self;



-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_objects = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"myProjects"]];
[_projectsCollectionView reloadData];

my cellForItemAtIndexPath:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ProjectCell *cell = (ProjectCell *)[cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
// Get the amount of subjects
NSMutableArray *subjects = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:_objects[indexPath.row

// Count the subjects
int subjectsCount = [subjects count];

// Set the text and integer value
cell.projectLabel.text = _objects[indexPath.row];
cell.projectCount.text = [NSString stringWithFormat:@"%i",subjectsCount];

cell.layer.cornerRadius = 16;
cell.layer.borderWidth = 1;
cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
// (note - may prefer to use the tintcolor of the control)

return cell;

User adds new item code:

// Ready to add the project, go ahead
[_objects insertObject:newProject atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[_projectsCollectionView insertItemsAtIndexPaths:@[indexPath]];

[[NSUserDefaults standardUserDefaults] setObject:_objects forKey:@"myProjects"];

Answer Source

Try add

cell.layer.shouldRasterize = YES;

It can help. Also all this staff:

cell.layer.cornerRadius = 16;
cell.layer.borderWidth = 1;
cell.layer.borderColor = [UIColor lightGrayColor].CGColor;

You should do in you custom cell class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download