UICollectionViewController:滚动时使用的内存增加

时间:2013-10-01 09:17:49

标签: ios objective-c memory storyboard uicollectionview

我有一个显示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

编辑: 我在仪器中分析了测试应用程序,这是在滚动时似乎增长的调用的深层副本:

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]

(......缩写...)

1 个答案:

答案 0 :(得分:0)

看起来UICollectionViewAccessibility(SafeCategory)中存在某种形式的错误,导致细胞重用无法正常工作。

有关更好的解释,请参阅此答案:UICollectionView do not reuse cells

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