ios - UICollectionViewController : Memory used increasing when scrolling

标签 ios objective-c memory storyboard uicollectionview

我有一个显示 UICollectionViewController 的 iPhone 应用程序。 Collection 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;
}

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

Interface Builder 中的 Collection 可重用 View 标识符 设置为 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/

相关文章:

ios - 强制转换会使共享扩展崩溃吗?

objective-c - 在 Xcode 中使用点击功能制作图像的最佳方式?

iOS ADBannerview - 广告会自行消失吗?

c - 计算字符串长度的不同方法

Python:在return语句后自动删除类对象

ios - Google Sign In SDK实例化VC为零

ios - 模态呈现时的交互式过渡

iphone - 如何让我的应用程序检测到按下主页按钮?

c++ - uint8_t vector 的内存地址

ios - 从 GeoJSON 中提取数据