2012-07-02 12 views

答えて

25

答えは実際には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 
    } 
} 

まだフラグについてはわかりませんが、

- (void)setContentSize:(CGSize)contentSize { 
    [super setContentSize:contentSize]; 
    // Do something. 
} 

うん、ちょうど親メソッドにアクセスするためにsuperを使用します。KVOを好きではない人のために

+0

これはもう少し前だと分かっていますが、ありがとうございます! – ryanwils

+11

'-removeObserver:forKeyPath:context:' – titaniumdecoy

+0

私は通常、 '-removeObserver:forKeyPath:context:'を '-dealloc'メソッドに入れます。 –

0

。これは、UIScrollview/TableView/CollectionViewのサブクラスを想定しています。

関連する問題