日々精進

新しく学んだことを書き留めていきます

複数のUITableViewのスクロールを同期させる

UITableViewをスクロールさせるとscrollViewDidScrollメソッドが呼ばれるらしいのでこれを使う。
まずスクロールしたらイベントが発生するようにする。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [[NSNotificationCenter defaultCenter] postNotificationName:SCROLL_NOTIFICATION object:scrollView];
}

スクロールイベントを受け取ったらスクロールイベントを発生させたTableとスクロール位置を合わせればOK

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveScrollNotification:) name:SCROLL_NOTIFICATION object:nil];
}

- (void)receiveScrollNotification:(NSNotification *)notification {
    UIScrollView *stockTableView = notification.object;
    self.tableView.contentOffset = stockTableView.contentOffset;
}

意外と簡単だった。