iOS 兩個tableView聯(lián)動效果的實現(xiàn)

1在storyboard中放好兩個tableView的布局


2 代碼部分

#import "TwoTablesViewController.h"

@interface TwoTablesViewController ()

@property (weak, nonatomic) IBOutlet UITableView *leftTableView;

@property (weak, nonatomic) IBOutlet UITableView *rightTableView;

@end

@implementation TwoTablesViewController {

NSArray *_leftArray;

NSArray *_rightArray;

}

- (void)viewDidLoad {

[super viewDidLoad];

_leftArray = [[NSArray alloc] initWithObjects:@"第一類",@"第二類",@"第三類",@"第四類",@"第五類",@"第六類",@"第七類",@"第八類", nil];

_rightArray = [[NSArray alloc] initWithObjects:@"一",@"二",@"三",@"四",@"五",@"六", nil];

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if (tableView == _rightTableView) {

return [_leftArray objectAtIndex:section];

}

return nil;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

if (tableView == _rightTableView) {

return [_leftArray count];

}

return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (tableView == self.leftTableView) {

return _leftArray.count;

}

else if (tableView == self.rightTableView) {

return _rightArray.count;

}

return 1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//? ? UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];

if (tableView == self.leftTableView) {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];

cell.textLabel.text = [_leftArray objectAtIndex:indexPath.row];

return cell;

}

else? {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell"];

cell.textLabel.text = [_rightArray objectAtIndex:indexPath.row];;

return cell;

}

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == _leftTableView) {

//? ? ? ? [_rightTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UITableViewScrollPositionTop animated:YES];

[_rightTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] animated:YES scrollPosition:UITableViewScrollPositionTop];

}

else {

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

}

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

NSLog(@"33333333");

if (scrollView == _rightTableView) {

NSIndexPath *indexPath = [[_rightTableView indexPathsForVisibleRows ] objectAtIndex:0];

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

}

}

//滑動停止時執(zhí)行

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

NSLog(@"33333333");

if (scrollView == _rightTableView) {

NSIndexPath *indexPath = [[_rightTableView indexPathsForVisibleRows ] objectAtIndex:0];

[_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

}

}

@end

3 運行效果


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市赏胚,隨后出現(xiàn)的幾起案子访娶,更是在濱河造成了極大的恐慌,老刑警劉巖觉阅,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件崖疤,死亡現(xiàn)場離奇詭異,居然都是意外死亡典勇,警方通過查閱死者的電腦和手機(jī)劫哼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來割笙,“玉大人权烧,你說我怎么就攤上這事∩烁龋” “怎么了实束?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵猬仁,是天一觀的道長。 經(jīng)常有香客問我,道長亥宿,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上孤里,老公的妹妹穿的比我還像新娘。我一直安慰自己橘洞,他們只是感情好捌袜,可當(dāng)我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著震檩,像睡著了一般琢蛤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上抛虏,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天博其,我揣著相機(jī)與錄音,去河邊找鬼迂猴。 笑死慕淡,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的沸毁。 我是一名探鬼主播峰髓,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼息尺!你這毒婦竟也來了携兵?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤搂誉,失蹤者是張志新(化名)和其女友劉穎徐紧,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體炭懊,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡并级,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了侮腹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嘲碧。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖父阻,靈堂內(nèi)的尸體忽然破棺而出愈涩,到底是詐尸還是另有隱情,我是刑警寧澤加矛,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布钠署,位于F島的核電站,受9級特大地震影響荒椭,放射性物質(zhì)發(fā)生泄漏谐鼎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望狸棍。 院中可真熱鬧身害,春花似錦、人聲如沸草戈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽唐片。三九已至丙猬,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間费韭,已是汗流浹背茧球。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留星持,地道東北人抢埋。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像督暂,于是被迫代替她去往敵國和親揪垄。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,665評論 2 354

推薦閱讀更多精彩內(nèi)容