ios - swift UITableView/ UICollectionView 调用观察(\.xxx)不起作用
问题描述
collection.addObserver(self, forKeyPath: #keyPath(UICollectionView.contentSize), options: .new, context: nil)
collection.observe(\.contentSize) { (collection, change) in
}
当我使用“addObserver”观察 contentSize 时,它起作用了,但是观察(.contentSize)不起作用,我不知道为什么。
解决方案
与此处的示例进行比较:
class MyObserver: NSObject {
@objc var objectToObserve: MyObjectToObserve
var observation: NSKeyValueObservation?
init(object: MyObjectToObserve) {
objectToObserve = object
super.init()
observation = observe(
\.objectToObserve.myDate,
options: [.old, .new]
) { object, change in
print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}
}
}
观察方法返回一个“观察”标记,你需要抓住它。它停止观察是否/何时超出范围。
推荐阅读
- twitter-bootstrap - 定义最大容器大小 [boostrap 5]
- orm - prisma:为什么我不能将“null”设置为可空列的默认值?
- java - 创建新列表作为列表
- > 在 Java 中
- html - 表单提交发送 POST 请求而不是 GET
- python - 如何在 Python/Django 中将 MP4 转换为具有多音频和多视频质量的 HLS?
- javascript - 使用样式组件的类型定义错误
- swift - Collectionview 动态高度变化,约束在 Swift 中不起作用
- php - 按类别名称(标题)对帖子进行排序(排序)
- javascript - 为什么将引导程序添加到角度无法正常工作?节点JS
- branch.io - 快速链接的唯一点击次数