Xcode 9 Swift 4 由于内存问题而终止(返回一个单元格)

Xcode 9 Swift 4 Terminated due to memory issue (returning a cell)

现在我知道还有很多其他问题存在同样的问题,但是,none 其中的问题在我的问题上崩溃了。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomTableViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.cellView.layer.cornerRadius = cell.cellView.frame.height / 2

    cell.animalLbl.text = elements[indexPath.row]
    cell.animalImage.image = UIImage(named: elements[indexPath.row])
    cell.animalImage.layer.cornerRadius = cell.animalImage.frame.height / 2

    return cell
}

return cell

那是导致我出现问题的行。我的阵列只有 10 个项目长,它在模拟器上运行良好。在我将所有图像添加到 table 视图之前,它在我的 iPad 上也运行良好。

这是我要求的数组:

let elements = ["The Perfect Apps", "Soft Boiled", "Medium Boiled", "Hard Boiled", "Scrambled","Fried", "Poached", "Rate", "Credits","Website"]

您似乎没有将 indexPath 传递到您的单元格中。您可能想改用它:

let cell = tableView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomTableViewCell

此外,您可能没有注册手机。这个总是让我绊倒并导致无法解释的崩溃(确保它在你的 viewDidLoad 中):

tableView.register(CustomTableViewCell.self, forCellWithReuseIdentifier: "customCell")

编辑: 在意识到您正在使用笔尖后,您可能想尝试将其作为您的寄存器单元。您可能想进入故事板并仔细检查您的笔尖是否命名为 CustomTableViewCell。如果不是,请将其更改为任何名称:

let nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
tableView.register(nib, forCellWithReuseIdentifier: "customCell")

最后,我的问题是我的图像文件太大,因此加载占用了太多内存。我所要做的就是降低它们的质量。