【问题标题】:iOS - Allocating custom UITableViewCell throwing NSMallocExceptioniOS - 分配自定义 UITableViewCell 抛出 NSMallocException
【发布时间】:2014-07-28 23:10:50
【问题描述】:

我正在使用 Swift 开发 iOS 应用程序,我知道该应用程序目前处于测试阶段,但到目前为止,我已经能够解决所有小问题,直到遇到这个问题。我有一个表格视图,它将由 FavoriteRowCell 类的自定义表格单元格填充。这是课程(我已经简化了,希望通过消除过程来解决它):

class FavoriteRowCell
:UITableViewCell {

    var data:ImageInfo?;

    init() {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "favoritesIdentifier");
    }

    func setData(data:ImageInfo) {
        self.data = data;
    }
}

这是我的 UITableViewDataSource 实现中的单元回收逻辑:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell:FavoriteRowCell? = tableView.dequeueReusableCellWithIdentifier("favoritesIdentifier") as? FavoriteRowCell;

    if(!cell) {
        cell = FavoriteRowCell();
    }

    var data:ImageInfo = favorites[indexPath!.row];
    cell!.setData(data);

    return cell!;
}

当我在模拟器中运行它时,一切正常。然而,当我在我的测试 iPod Touch 上运行它时,我得到一个 NSMallocException 并且应用程序崩溃了。当它尝试创建 FavoriteRowCell 对象时会发生这种情况,但我不知道为什么。

有没有人遇到过类似的事情,或者有什么你能看出我可能做错了吗?

【问题讨论】:

  • 你在使用故事板吗?
  • 我不是。带有以编程方式创建的 UI 的空 iOS 项目。

标签: ios uitableview malloc swift


【解决方案1】:

尝试使用以下方法创建您的单元格:

cell = FavoriteRowCell(style: UITableViewCellStyle.Default, reuseIdentifier: "favoritesIdentifier")

【讨论】:

  • 我修改了我的构造函数,以便可以像这样创建它。很遗憾没有变化。
【解决方案2】:

我相信我找到了问题的原因。 ImageInfo 类型是一个结构,而不是一个类,所以我猜这是导致问题的原因。

【讨论】:

    猜你喜欢
    • 2013-05-02
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    相关资源
    最近更新 更多