UICollectionView Cell Iteration - Memory Warning

After debugging, I found that the following code forces my application to use memory constantly until the application crashes. When I run the application, the memory usage is about 2 MB, and then when I continue to scroll back and forth, it continues to grow. When I stop scrolling for a second, the memory usage decreases (not critically, although, for example, from 50 M to 35 M). Please see why I:

This happens in my subclass of UICollectionViewController:

for (myAppCell *cell in [self.collectionView visibleCells])
{
    UILabel *label [[UILabel alloc] initWithFrame:CGRectMake(10, 10, cell.frame.size.width, cell.frame.size.height)];
    label.text = @"Hello World!";

    [cell.contentView addSubview:label];
}

Now here are the observations I made while debugging:

  • If I comment all the code inside the block, and scroll back and forth for infinite time, the application memory is 2.1 MB stable (in this case, it continues the loop, but actually does nothing inside, so the problem should be with the UILabel distribution).
  • I tried to check the count of subviews of both the collection view and the cells, and they are respectively 14 and 1 (as it should be - the problem is not that I think it will be accumulation of subviews).
  • ARC is on.
  • Edit : Actually, the problem only occurs when adding a UILabel to the cell. When I comment out the last line ( [cell.contentView addSubview:label), the application also works fine. Therefore, it should be this line.
  • . , , ( , , , , iOs).

, , , , - , , ( 2.1 ).

?

CustomCollectionViewController reloadData. scrollViewDidEndDecelerating.

+4
1

, , UILable . . .

 [[cell.contentView subviews]
     makeObjectsPerformSelector:@selector(removeFromSuperview)];
+5

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


All Articles