【问题标题】:NSMutableArray Datasrouce TableView UI Freeze after scrollNSMutableArray Datasrouce TableView UI 滚动后冻结
【发布时间】:2016-08-01 17:57:44
【问题描述】:

我正在使用 XCode 8.0 版为 iOS 10 进行开发。

在我的 .h 文件中,我有

@property (nonatomic, strong) NSMutableArray *items;

这是我的 TableView 的数据源。

在 viewDidLoad 中,我调用我的函数 [self setupTableViewItems];

self.items = [[NSMutableArray alloc] initWithCapacity:4];
self.items = [CellModel rows];

这个数组中的每个对象都代表我的单元格,例如:

CellModel *screenshot = [[CellModel alloc]
                            initWithParams:         @"Screenshot"
                            subTitle:               @"Select a screenshot"
                            image:                  [UIImage imageNamed: @"screenshot"]
                            viewName:               Pictures
                            section:                3];

这里是方法签名,

+ (NSMutableArray *)rows;

以及重要的实现细节(每个对象都是 CellModel 类型):

NSMutableArray *screenshots = [[NSMutableArray alloc] initWithCapacity:3];
[screenshots addObject:quickScreenshot];
[screenshots addObject:selectScreenshot];
[screenshots addObject:selectedScreenshotsCollectionView];

.....

NSMutableArray *dataSource = [[NSMutableArray alloc] initWithCapacity:4];
[dataSource addObject:interactions];
[dataSource addObject:screenshots];
[dataSource addObject:problem];
[dataSource addObject:details];

return [dataSource mutableCopy];

tableView 首次正确加载数据,视图看起来很棒,没有任何遗漏。

当我开始滚动时,我立即注意到单元格的附件视图开始混淆,尤其是在第 3 节,它们被定义为没有辅助视图,但仍然混淆并获得其他单元格的辅助视图。

但是,一旦方向改变,或者我开始上下滚动几次,UI 就会被锁定,没有崩溃,也没有来自 XCode 的消息。内存在增长,这显然是一个泄漏,但在试图找到它几个小时后,我绝望了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GenericTableViewCell *cell = [tableView
                                  dequeueReusableCellWithIdentifier:REPORT_CELL
                                  forIndexPath: indexPath];

CellModel *current = [[self.items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];


cell.titleLabel.text = current.title;
cell.subtitleLabel.text = current.subTitle;
cell.icon.image = current.image;

    if(indexPath.section == 0 && indexPath.row == 0)
        cell.accessoryView = self.lastInteractionSwitch;
    else if(indexPath.section == 0 && indexPath.row == 1)
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    else if(indexPath.section == 1 && indexPath.row == 0)
        cell.accessoryView = self.lastScreenshotSwitch;
    else if(indexPath.section == 1 && indexPath.row == 1)
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    else if(indexPath.section == 2 && indexPath.row == 0)
        cell.accessoryView = self.starRatingView;
    else if(indexPath.section == 2 && indexPath.row == 1)
        cell.accessoryView = self.speechToTextButton;
    else cell.accessoryView = UITableViewCellAccessoryNone;
return cell;

我的自定义单元格具有三个正在设置的属性。自定义单元格为“字幕”类型,属性在IB中连接。

首先,我认为这是因为我没有指针引用,添加它,尝试使用直观和明显的引用(强 || 保留),然后将 NSArray 作为数据源,同样的问题。更改了单元格(改为 100% 苹果,100% 自定义),同样的问题。

这里发生了什么?

【问题讨论】:

  • 你提到附件视图是什么变得一团糟,但我没有看到你在出列单元格的cellForRowAtIndexPath 中的任何地方设置附件。您是否在显示单元格之前在任何地方设置此属性?另外,表格(及其单元格)是屏幕上唯一的 UI 元素吗?
  • 是的。我为您更新了我的 cellForRowAtIndexPath: 定义。我更改了导航栏的 barTintColor,将其设置为半透明,将文本设置为白色,并添加一个右键项目。除此之外,我只是展示一个tableview,没有别的。
  • UI 锁定并且内存增长 = 您在主线程上有无限递归/循环(可能)。使用调试器运行它,当它发生时 - 只需在调试器中按暂停。如果您的代码没有停止 - 多做几次。运气好吗?
  • 不是没有代表我发生递归,宅男;)。有趣的是,内存增长来自我的收藏视图,它无法满足我想要的内容(截图),我不断收到警告,自动布局也在这里吓坏了,内存不断增长。 @StepanGeneralov

标签: ios objective-c uitableview


【解决方案1】:

单元格的行高是多少?

您是否还检查了第 3 节中该对象的数据?

CellModel *current = [[self.items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.titleLabel.text = current.title;
cell.subtitleLabel.text = current.subTitle;
cell.icon.image = current.image;

【讨论】:

  • 嘿!我返回 66;在 heightForRowAtIndexPath 中,在 IB 中也设置为 66。这也可能是单元格问题的高度,因为在更改其值后,我开始发生一些奇怪的事情。
  • @AdamGartner 改变高度后发生了什么?
  • 原来是自动布局探针。我正在覆盖 heightforrowatindexpath,与苹果单元相结合,但具有自定义属性和高度。小心身高的孩子,这对我来说浪费了很多时间。 TLDR:设计一个带有自动布局属性的漂亮自定义单元格,让苹果系统开发人员为您完成其余的工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多