【发布时间】:2012-07-02 21:17:02
【问题描述】:
是否有一种自动方式来了解 UITableView/UIScrollView 中的 ContentSize 变化?
【问题讨论】:
-
什么需要注意内容大小的变化? “
UITableView”和“UIScrollView”对象本身,或者包含它们的东西,还是其他东西?
标签: ios uitableview uiscrollview
是否有一种自动方式来了解 UITableView/UIScrollView 中的 ContentSize 变化?
【问题讨论】:
UITableView”和“UIScrollView”对象本身,或者包含它们的东西,还是其他东西?
标签: ios uitableview uiscrollview
答案其实很简单,使用KVO(Key Value Observing);
- (id)initWithFrame:(CGRect)frame tableView:(UITableView *)tableView
{
// ....
[self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionPrior context:NULL];
// ....
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentSize"])
{
// Do something
}
}
不过我还不确定这些标志。
【讨论】:
-removeObserver:forKeyPath:context:
-removeObserver:forKeyPath:context:放在-dealloc方法中。
对于那些不喜欢 KVO 的人:
- (void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
// Do something.
}
是的,只需使用super 访问父方法。这假设你继承 UIScrollview/TableView/CollectionView
【讨论】: