ios - Swift 中奇怪的协议行为
问题描述
我的应用程序中有一个协议:
public protocol ViewModel {}
public protocol ConfigurableView where Self: UIView {
func configure(with viewModel: ViewModel)
}
显然,这样做的目的是标准化我的 ViewModel 方法。
当我在 cellForRowAt: 方法中应用配置调用时:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableViewCellType(for: indexPath).reuseIdentifier, for: indexPath)
let item = sections[indexPath.section][indexPath.item]
let viewModel = process(item: item)
(cell as? ConfigurableCell)?.configure(item:item)
return cell
}
我看到了奇怪的行为 - 本质上,单元格的类型是正确的,并且在此方法中具有我期望的属性,但是一旦将其传递到 configure 方法中,其所有自定义属性(UILabels 等)都为零。这就像它复制了单元格而不转移所有属性。一旦代码进入配置方法的内部,它就完全是一个不同的指针。
我在这里想念什么?我试过在操场上重新创建它,但我做不到,这让我怀疑出队细胞很奇怪。
解决方案
推荐阅读
- python - 如何使用 Poetry 从依赖项中发送可执行文件?
- python - 当向相反方向推进时,如何正确地对自由漂浮的空间物体实施重力和某种摩擦
- javascript - 使用逗号运算符将 javascript 函数转换为 python
- python - 在用户定义的类中的任意属性上调用方法
- python - 为什么我在 Python 中遇到错误,例如 Dog() 没有参数?
- selectors-api - querySelectorAll 的点击按钮问题
- python - Python 3,以及带逗号的切片操作数
- php - 对于 ALTER INDEX all ON tbl REORGANIZE 查询,sqlsrv_query 返回什么?为什么 sqlsrv_num_rows 返回 FALSE?
- python - 如何使用以下元素在硒中定位元素?
- flutter - 在颤振中使用 image_picker 拍摄多张照片