【发布时间】:2019-04-03 18:20:11
【问题描述】:
我必须展示几个不同的单元格。我为此调用了tableView(_:cellForRowAt:),在该方法中,我为UITableViceCell的不同类别使用了两个不同的ID
这是一个简单的代码:
class SimpleView: UITableViewController {
...
let cellIdMain = "JournalMainCellID"
let cellIdExtra = "JournalMainSceneAddNewID"
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == journals.count {
guard let cellAdding = tableView.dequeueReusableCell(withIdentifier: cellIdExtra, for: indexPath) as? JournalMainSceneAddNew else {
fatalError("Cannot connect to the cell")
}
return cellAdding
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdMain, for: indexPath) as? JournalMainSceneCell else {
fatalError("Cannot connect to the cell")
}
cell.numberOfCountriesLabel.text = "\(journals[indexPath.row].numberOFCountries)"
return cell
}
}
当我试图查找内存泄漏时,我发现:
当我点击我找到的详细信息时:
为什么会这样?它看起来非常简单明了。
更新:图片已更新。
【问题讨论】:
-
是的,但是 XCode 指出了代码的不同部分。
-
@matt 抱歉,这是真的。它是相同的。 Xcode 显示每行 32 字节的内存泄漏。我会更新图片。
-
@matt - 然后我在
tableView(_:cellForRowAt:)中使用字符串值而不是cellIdMain或cellIdExtra看起来内存泄漏消失了...奇怪,所有ID 都是let常量... -
我也试过使用
guard let cellAdding = tableView.dequeueReusableCell(withIdentifier: "\(cellIdExtra)", for: indexPath) else...,没有任何效果。
标签: ios swift memory-leaks