Kir*_*ira 7 uitableview ios swift prepareforreuse
需要帮助了解如何在UIKit中使用prepareForReuse().该文件说:
您应该只重置与内容无关的单元格属性,例如,alpha,编辑和选择状态
但重置个别属性属性如isHidden呢?
假设我的单元格有2个标签,我应该重置:
我的tableView(_:cellForRowAt :)委托有条件逻辑来隐藏/显示每个单元格的标签.
Hon*_*ney 20
引用Apple自己的文档:
出于性能原因,您应该仅重置与内容无关的单元格属性,例如,alpha,编辑和选择状态.
例如,如果选择了一个单元格,您只需将其设置为未选中,如果您将背景颜色更改为某些内容,则只需将其重置为默认颜色即可.
表视图的委托
tableView(_:cellForRowAt:)应始终在重用单元格时重置所有内容.
这意味着如果您尝试设置联系人列表的配置文件图像,则不应尝试进入nil图像prepareforreuse,应正确设置图像cellForRowAt,如果没有找到任何图像,则将图像设置nil为默认值或默认值图片.基本上cellForRowAt应该控制预期/意外状态.
所以基本上不建议以下内容:
override func prepareForReuse() {
super.prepareForReuse()
imageView?.image = nil
}
Run Code Online (Sandbox Code Playgroud)
而是推荐以下内容:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
cell.imageView?.image = image ?? defaultImage // unexpected situation is also handled.
// We could also avoid coalescing the `nil` and just let it stay `nil`
cell.label = yourText
cell.numberOfLines = yourDesiredNumberOfLines
return cell
}
Run Code Online (Sandbox Code Playgroud)
此外,建议使用以下默认的非内容相关项:
override func prepareForReuse() {
super.prepareForReuse()
isHidden = false
isSelected = false
isHighlighted = false
}
Run Code Online (Sandbox Code Playgroud)
这是Apple建议的方式.但说实话,我仍然认为cellForRowAt像Matt所说的那样将所有东西都倾倒在内部更容易.清洁代码很重要,但这可能无法帮助您实现这一目标.但是,正如Connor说的那样,唯一需要的是,如果你需要取消正在加载的图像.有关详情,请参阅此处
即做一些事情:
override func prepareForReuse() {
super.prepareForReuse()
imageView.cancelImageRequest() // this should send a message to your download handler and have it cancelled.
imageView.image = nil
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8437 次 |
| 最近记录: |