一绷跑、tableView雙級聯(lián)動
以上兩種效果比較類似,實現(xiàn)的關鍵在于都是需要獲得在滑動過程中滑動到tableView頂部的cell的indexPath氮发。
方案一:獲得當前可見的所有cell渴肉,然后取可見cell數(shù)組中的第一個cell就是目標cell,再根據(jù)cell獲得indexPath爽冕。代碼如下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == _rightTableView && _isSelected == NO) {
//返回tableView可見的cell數(shù)組
NSArray * array = [_rightTableView visibleCells];
//返回cell的IndexPath
NSIndexPath * indexPath = [_rightTableView indexPathForCell:array.firstObject];
NSLog(@"滑到了第 %ld 組 %ld個",indexPath.section, indexPath.row);
_currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[_leftTableView reloadData];
[_leftTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
方案二(推薦使用):利用偏移量仇祭!偏移量的值實際上可以代表當時處于tableView頂部的cell在tableView上的相對位置, 那么我們就可以根據(jù)偏移量獲得處于頂部的cell的indexPath颈畸。代碼如下
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == _rightTableView && _isSelected == NO) {
//系統(tǒng)方法返回處于tableView某坐標處的cell的indexPath
NSIndexPath * indexPath = [_rightTableView indexPathForRowAtPoint:scrollView.contentOffset];
NSLog(@"滑到了第 %ld 組 %ld個",indexPath.section, indexPath.row);
_currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[_leftTableView reloadData];
[_leftTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
二乌奇、 獲取處于UITableView中心的cell
獲取處于tableView中間cell的效果,用上述方案一比較麻煩:要考慮可見cell 的奇眯娱、偶個數(shù)問題礁苗,還有cell是否等高的情況;方案二用起來就快捷方便多了徙缴,取的cell的位置的縱坐標相當于在偏移量的基礎上又增加了tableView高度的一半寂屏。代碼如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//獲取處于UITableView中心的cell
//系統(tǒng)方法返回處于tableView某坐標處的cell的indexPath
NSIndexPath * middleIndexPath = [_rightTableView indexPathForRowAtPoint:CGPointMake(0, scrollView.contentOffset.y + _rightTableView.frame.size.height/2)];
NSLog(@"中間的cell:第 %ld 組 %ld個",middleIndexPath.section, middleIndexPath.row);
}
目前能想到的也就這了,各位同僚有什么好的想法歡迎在此留言交流娜搂。不喜勿噴G!百宇!謝謝