【发布时间】:2018-07-11 14:28:47
【问题描述】:
我已在仪器“泄漏”工具中检查了我的应用程序是否存在内存泄漏 并在负责 UITableView 内容的控制器上找到了其中的 16 个。
这是代码 -
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "clause", for: indexPath) as! CodeOfConductTableViewCell
cell.index.text = String(indexPath.row + 1)
cell.cocTxt.text = lines["line\(indexPath.row + 1)"]
return cell
}
当我把代码改成这样时:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = CodeOfConductTableViewCell()
cell.index = UILabel()
cell.cocTxt = UITextView()
cell.index?.text = String(indexPath.row + 1)
cell.cocTxt?.text = lines["line\(indexPath.row + 1)"]
return cell
}
未检测到泄漏。
是什么导致了泄漏?我用错了吗?
我想使用这个功能,因为它对性能更好, 我想了解这里出了什么问题
谢谢!
编辑:
添加单元类代码(这里没什么花哨的):
class CodeOfConductTableViewCell: UITableViewCell {
@IBOutlet weak var index: UILabel!
@IBOutlet weak var cocTxt: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
【问题讨论】:
-
“更好的性能”是什么意思?
-
在第一个示例中,您如何在单元格中创建标签和文本视图?变量是否声明为弱?
-
@skladek 是的,它们被宣布为弱出口。没有特殊的初始化..
-
@ChipCoons 不是创建新单元格,而是重用屏幕外的单元格。如果我有一个很大的表,如果我继续创建新单元格,应用程序将耗尽内存。
-
行包含什么?
标签: ios swift uitableview memory-leaks swift4