4

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

考虑以下测试代码:

- (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;
}

当我运行应用程序并在集合视图中上下滚动时,XCode 5s 调试视图 (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]

(...缩写...)

4

1 回答 1

0

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

请参阅此答案以获得更好的解释:UICollectionView 不重用单元格

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

于 2013-12-02T12:25:12.853 回答