讓我們共同學(xué)習(xí)一下tableView聯(lián)動,我這也是從簡書上看來的一篇文章弓柱,來親自實現(xiàn)一下。學(xué)習(xí)文章地址:http://www.reibang.com/p/dfb73aa08602
先上圖:
功能需求(兩點):
- 點擊左邊tableVIew的cell侧但,右邊的tableView滑動至指定位置矢空。
- 滑動右邊tableView的cell,左邊的tableView滑動至指定位置禀横。
具體思路:
- 實現(xiàn)點擊左邊tableView同時滾動右邊tableView屁药,只需要實現(xiàn)tableView的代理方法。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
然后在代理方法里邊拿到右邊的tableView柏锄,實現(xiàn)讓其滾動到第indexPath.row分區(qū)酿箭,第0行即可,代碼如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 如果點擊的是右邊的tableView趾娃,不做任何處理
if (tableView == self.rightTableView) return;
// 點擊左邊的tableView七问,設(shè)置選中右邊的tableView某一行。
左邊的tableView的每一行對應(yīng)右邊tableView的每個組第0行
[self.rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
我們這里不處理右邊tableView的點擊事件茫舶,所以
if (tableView == self.rightTableView) return;
接下來,拖動右邊的tableView刹淌,來找到左邊tableView對應(yīng)的某一行饶氏。
我們要動態(tài)選中左邊的tableView讥耗,就需要拿到右邊tableView現(xiàn)在滾動到了那個組。UITableView有兩個代理方法:
// 一個頭標(biāo)題即將顯示的時候調(diào)用
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
// 一個頭標(biāo)題即將消失的時候掉用
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section
利用這兩個方法就可以拿到當(dāng)前所在哪個組實現(xiàn)這個功能了疹启。
還有一個更簡單的方法古程,在tableView中,是不常用的喊崖,叫做挣磨,indexPathsForVisibleRows官方文檔解釋是:
The value of this property is an array of NSIndexPath objects each representing a row index and section index that together identify a visible row in the table view. If no rows are visible, the value is nil.
大概意思是:返回一個屏幕上可見的cell的indexPath集合
好的,重點來了荤懂,拿到這個集合茁裙,不就能拿到目前屏幕上頂端的cell的indexpath了嗎,那就如愿以償?shù)哪玫浆F(xiàn)在所在第indexpath.section個分區(qū)了节仿。
上代碼:
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 監(jiān)聽tableView滑動
// 如果現(xiàn)在滑動的是左邊的tableView晤锥,不做任何處理
if ((UITableView *)scrollView == self.leftTableView) return;
// 滾動右邊tableView,設(shè)置選中左邊的tableView某一行廊宪。
indexPathsForVisibleRows屬性返回屏幕上可見的cell的indexPath數(shù)組矾瘾,
利用這個屬性就可以找到目前所在的分區(qū)
[self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.rightTableView.indexPathsForVisibleRows.firstObject.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
[詳解]:
- 首先監(jiān)聽scrollView的拖動,本demo不處理左邊tableView的滾動箭启,所以
if ((UITableView *)scrollView == self.leftTableView) return; - 下一句代碼的意思是壕翩,拿到當(dāng)前屏幕上可見cell的第一行cell所在的分區(qū),然后讓左邊的tableView選中第0分區(qū)(它只有一個分區(qū))的這一行就OK了傅寡。
self.rightTableView.indexPathsForVisibleRows.firstObject.section
demo地址:https://github.com/RenZhengYang/ZYLinkageTableView
--------------------------補充-------------------------------
1.點擊左邊tableView的時候會有陰影效果
- 點擊左邊的tableView放妈,右邊的tableView是從當(dāng)前位置動畫滾動到相應(yīng)位置的,既然有滾動赏僧,就會調(diào)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
這個代理方法大猛,說白了就是拖動了右邊tableView,拖動右邊的過程中會陸續(xù)選中左邊淀零。
如果不想要這個效果挽绩,有兩個辦法,一個是直接把
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
中的動畫滾動的屬性animated值改成NO**
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 如果點擊的是右邊的tableView驾中,不做任何處理
if (tableView == self.rightTableView) return;
// 點擊左邊的tableView唉堪,設(shè)置選中右邊的tableView某一行。
左邊的tableView的每一行對應(yīng)右邊tableView的每個分區(qū)
[self.rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
這樣做右邊的tableView就是無動畫滾動了肩民,也就不會再調(diào)scrollViewDidScroll:方法唠亚。
但是如果還想右邊tableViewyou滾動效果,另一種解決方法是把
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
方法換成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
這個代理方法方法就行了持痰。有的界面好像就是這樣做的灶搜,但是有bug(估計餓了么沒測出來),這個方法的注釋為
// called when scroll view grinds to a halt 當(dāng)滾動視圖戛然而止--有道翻譯如是說
這個方法調(diào)用與否在于你的手指是否在動畫停止之前離開了屏幕,如果在動畫結(jié)束之前手指離開屏幕割卖,此方法調(diào)用沒什么問題前酿。but,如果動畫已經(jīng)停止鹏溯,再把手指拿開罢维,這個方法是不會調(diào)的。
解決這個bug的關(guān)鍵在于丙挽,讓手指離開的時候手動調(diào)一次這個代理方法肺孵,那怎么才能知道手指什么時候離開呢?scrollView給我們了另一個代理方法:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
這個方法在結(jié)束拖拽的時候調(diào)颜阐,正好解決了我們的問題:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
// 推拽將要結(jié)束的時候手動調(diào)一下這個方法
[self scrollViewDidEndDecelerating:scrollView];
}
-
學(xué)習(xí)小記:
1.// 默認(rèn)選擇左邊tableView的第一行
[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
必須在tableView數(shù)據(jù)加載完畢后平窘,才會調(diào)用。否則會報錯瞬浓。
本篇文章初婆,與學(xué)習(xí)作者文章相同,只不過排版猿棉,被我改了磅叛,demo中的寫法,被我改了萨赁。整理一下弊琴,學(xué)習(xí)備用。若有侵權(quán)杖爽,請及時聯(lián)系敲董,我會做更改。如有欠缺慰安,或有好的想法腋寨,請即時提出』溃互相交流學(xué)習(xí)萄窜,歡迎點星,鼓勵H鼋啊2榭獭!。