UICollectionViewController

UICollectionViewController

我有一个显示 UICollectionViewController 的 iPhone 应用程序。集合 View 包含具有 UILabel 作为 subview 的单元格。

考虑以下测试代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:1];
    label.text = [NSString stringWithFormat:@"%d", indexPath.row];

    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:    (NSInteger)section
{
    return 500;
}

当我运行应用程序并在集合 View 中上下滚动时,XCode 5s 调试 View (CMD-6) 中显示的内存使用量将稳定增加。这是预期的行为还是我在某处有泄漏?

Interface Builder 中的 Collection reusable view identifier 设置为 Cell

编辑:
我在 Instruments 中分析了测试应用程序,这是滚动时似乎增长的调用的深拷贝:
Bytes Used  Count       Symbol Name
   1.01 MB      24.2%   24251       +[NSObject allocWithZone:]
 229.95 KB       5.3%   2774         -[UINibDecoder decodeObjectForKey:]
 173.25 KB       4.0%   2376          -[UIView initWithCoder:]
 111.38 KB       2.6%   1980           -[UICollectionReusableView initWithCoder:]
 111.38 KB       2.6%   1980            -[UICollectionViewCell initWithCoder:]
 111.38 KB       2.6%   1980             -[UINibDecoder decodeObjectForKey:]
 111.38 KB       2.6%   1980              -[UINib instantiateWithOwner:options:]
 111.38 KB       2.6%   1980               -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
 111.38 KB       2.6%   1980                -[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
 111.38 KB       2.6%   1980                 -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]
 111.38 KB       2.6%   1980                  -[CVTViewController collectionView:cellForItemAtIndexPath:]
 111.38 KB       2.6%   1980                   -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
 111.38 KB       2.6%   1980                    -[UICollectionView _updateVisibleCellsNow:]
 111.38 KB       2.6%   1980                     -[UICollectionView layoutSubviews]
 111.38 KB       2.6%   1980                      -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
 111.38 KB       2.6%   1980                       -[NSObject performSelector:withObject:]
 111.38 KB       2.6%   1980                        -[CALayer layoutSublayers]
  61.88 KB       1.4%   396        -[UINibDecoder decodeObjectForKey:]
  61.88 KB       1.4%   396         -[UIView initWithCoder:]
  61.88 KB       1.4%   396          -[UICollectionReusableView initWithCoder:]
  61.88 KB       1.4%   396           -[UICollectionViewCell initWithCoder:]
  61.88 KB       1.4%   396            -[UINibDecoder decodeObjectForKey:]
  61.88 KB       1.4%   396             -[UINib instantiateWithOwner:options:]
  61.88 KB       1.4%   396              -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
  61.88 KB       1.4%   396               -[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
  61.88 KB       1.4%   396                -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]
  61.88 KB       1.4%   396                 -[CVTViewController collectionView:cellForItemAtIndexPath:]
  61.88 KB       1.4%   396                  -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
  61.88 KB       1.4%   396                   -[UICollectionView _updateVisibleCellsNow:]
  61.88 KB       1.4%   396                    -[UICollectionView layoutSubviews]
  61.88 KB       1.4%   396                     -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
  61.88 KB       1.4%   396                      -[NSObject performSelector:withObject:]
  61.88 KB       1.4%   396                       -[CALayer layoutSublayers]
  55.69 KB       1.3%   396       -[UINib instantiateWithOwner:options:]
  55.69 KB       1.3%   396        -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
  55.69 KB       1.3%   396         -[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
  55.69 KB       1.3%   396          -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]
  55.69 KB       1.3%   396           -[CVTViewController collectionView:cellForItemAtIndexPath:]
  55.69 KB       1.3%   396            -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:]
  55.69 KB       1.3%   396             -[UICollectionView _updateVisibleCellsNow:]
  55.69 KB       1.3%   396              -[UICollectionView layoutSubviews]
  55.69 KB       1.3%   396               -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
  55.69 KB       1.3%   396                -[NSObject performSelector:withObject:]
  55.69 KB       1.3%   396                 -[CALayer layoutSublayers]

(...缩写...)

最佳答案

看起来 UICollectionViewAccessibility(SafeCategory) 中存在某种形式的错误,阻止单元重用正常工作。

请参阅此答案以获得更好的解释:UICollectionView do not reuse cells

另外..您使用什么设备来解决此问题?

关于ios - UICollectionViewController : Memory used increasing when scrolling,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19112347/

10-13 08:50