讓我們共同學(xué)習(xí)一下tableView聯(lián)動,我這也是從簡書上看來的一篇文章,來親自實現(xiàn)一下。學(xué)習(xí)文章地址:http://www.lxweimin.com/p/dfb73aa08602
先上圖:
功能需求(兩點(diǎn)):
- 點(diǎn)擊左邊tableVIew的cell,右邊的tableView滑動至指定位置。
- 滑動右邊tableView的cell,左邊的tableView滑動至指定位置。
具體思路:
- 實現(xiàn)點(diǎ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{
// 如果點(diǎn)擊的是右邊的tableView,不做任何處理
if (tableView == self.rightTableView) return;
// 點(diǎn)擊左邊的tableView,設(shè)置選中右邊的tableView某一行。
左邊的tableView的每一行對應(yīng)右邊tableView的每個組第0行
[self.rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
我們這里不處理右邊tableView的點(diǎn)擊事件,所以
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集合
好的,重點(diǎn)來了,拿到這個集合,不就能拿到目前屏幕上頂端的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
--------------------------補(bǔ)充-------------------------------
1.點(diǎn)擊左邊tableView的時候會有陰影效果
- 點(diǎn)擊左邊的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{
// 如果點(diǎn)擊的是右邊的tableView,不做任何處理
if (tableView == self.rightTableView) return;
// 點(diǎn)擊左邊的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í),歡迎點(diǎn)星,鼓勵!!!。