iOS實現(xiàn)可以縱向橫向滑動的表格實例代碼
本文主要給大家介紹了關(guān)于iOS實現(xiàn)可以縱向橫向滑動的表格的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
效果圖
這個效果是今天公司項目里面遇上的,也是第一次遇見這種需求,所以記錄下來,效果如上圖。需求主要是可以實現(xiàn)上下的滑動,并且同時最左側(cè)的“線路名稱”這一列在向左滑動的時候是不能跟隨滾動的。這個功能主要是實現(xiàn)用戶可以方便查看關(guān)于一下難以看全的列表數(shù)據(jù)。下面說一下思路。
代碼大體思路
由上面的GIF圖和基本需求描述我們第一個想到的東西就是萬能的tableview,沒錯,這個功能的完成當(dāng)然離不開tableview,那么tableview應(yīng)該怎樣發(fā)揮它的功力呢,左右側(cè)的信息需要對稱,所以在這里我使用了兩個tableview,也就是最左側(cè)線路名稱這一列是一個tableview,右側(cè)的粉紅色字體這些行是一個tableview。上下滑動兩者關(guān)聯(lián)是使用scrollview完成的。那接下來就結(jié)合代碼簡單說一下,也方便我以后回頭看,哈哈哈。
代碼解析
1、這是需要的原材料,每個變量都有注釋它的功能了,一眼懂。titleTableView是最左側(cè)的線路名稱這一列。infoTableView是粉紅色字體這些。contentView是titleTableView和最上方(除了“線路名稱”)這一列內(nèi)容的superView。
@property (nonatomic, strong) UITableView *titleTableView;//標(biāo)題TableView @property (nonatomic, strong) UITableView *infoTableView;//內(nèi)容TableView @property (nonatomic, strong) UIScrollView *contentView;//內(nèi)容容器 @property (nonatomic, strong) NSArray *infoArr;//數(shù)組 @end @implementation ViewController { CGFloat _kOriginX; CGFloat _kScreenWidth; CGFloat _kScreenHeight; }
2、這是所需要的數(shù)據(jù)配置,我把里面所有需要的數(shù)據(jù)都放在數(shù)組李典里面了。我比較懶。哈哈哈哈
- (void)configData { _kOriginX = 120; _kScreenWidth = self.view.frame.size.width; _kScreenHeight = self.view.frame.size.height; _infoArr = @[@{@"title":@"出團日期", @"routeName":@"線路名稱一", @"time":@"2015/11/21", @"num":@"20", @"price":@"124.0", @"code":@"DAGSDSASA"}, @{@"title":@"余位", @"routeName":@"線路名稱二", @"time":@"2015/11/21", @"num":@"34", @"price":@"234", @"code":@"TAGDFASFAF"}, @{@"title":@"價格", @"routeName":@"線路名稱三", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"}, @{@"title":@"團代號", @"routeName":@"線路名稱四", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}]; }
3、分步來看,首先是頭部的,這個titleLabel是最左上角的“線路名稱”這四個字,contentView的配置,上面說了這個contentView的作用的,從它的frame看出來, _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(_kOriginX, 0, _kScreenWidth - _kOriginX, _kScreenHeight)];
它的x是_kOriginX也就是預(yù)留的最左側(cè)的空間。最上面的一列使用for循環(huán)創(chuàng)建出來的label。
//MARK:- 頭部視圖 - (void)configTableHeader { UILabel *titleLabel = [self quickCreateLabelWithLeft:0 width:_kOriginX title:@"線路名稱"]; [self.view addSubview:titleLabel]; _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(_kOriginX, 0, _kScreenWidth - _kOriginX, _kScreenHeight)]; _contentView.delegate = self; _contentView.showsVerticalScrollIndicator = NO; _contentView.showsHorizontalScrollIndicator = NO; _contentView.contentSize = CGSizeMake(400, _kScreenHeight); _contentView.bounces = NO; [self.view addSubview:_contentView]; for (int i = 0; i < _infoArr.count; i++) { CGFloat x = i * 100; UILabel *label = [self quickCreateLabelWithLeft:x width:100 title:[[_infoArr objectAtIndex: i] objectForKey:@"title"]]; label.textAlignment = NSTextAlignmentCenter; [_contentView addSubview:label]; } }
4、那接下來就是配置最左側(cè)那一欄和左側(cè)粉紅色字體那些行。也就這兩個tableview創(chuàng)建的。
//MARK:- 詳細(xì)內(nèi)容 - (void)configInfoView { _titleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, _kOriginX, _kScreenHeight) style:UITableViewStylePlain]; _titleTableView.dataSource = self; _titleTableView.delegate = self; _titleTableView.showsVerticalScrollIndicator = NO; _titleTableView.showsHorizontalScrollIndicator = NO; _titleTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:_titleTableView]; _infoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 400, _kScreenHeight) style:UITableViewStylePlain]; _infoTableView.delegate = self; _infoTableView.dataSource = self; _infoTableView.showsVerticalScrollIndicator = NO; _infoTableView.showsHorizontalScrollIndicator = NO; _infoTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [_contentView addSubview:_infoTableView]; }
5、這是tableview的代理方法實現(xiàn)。在cellForRowAtIndexPath這個代理方法中,將兩個tableview的cell分開來寫。
//MARK:- UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _infoArr.count; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == _titleTableView) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleTable"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"titleTable"]; } cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.text = [[_infoArr objectAtIndex:indexPath.row] objectForKey:@"routeName"]; cell.textLabel.textColor = [UIColor lightGrayColor]; cell.textLabel.font = [UIFont systemFontOfSize:14]; if (indexPath.row%2 == 1) { cell.backgroundColor = [UIColor colorWithRed:218/255.0 green:218/255.0 blue:218/255.0 alpha:1]; } else { cell.backgroundColor = [UIColor whiteColor]; } return cell; } else { NSString *ident = @"InfoCell"; InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:ident]; if (!cell) { cell = [[[NSBundle mainBundle] loadNibNamed:@"InfoCell" owner:nil options:nil] lastObject]; } if (indexPath.row%2 == 1) { cell.backgroundColor = [UIColor colorWithRed:218/255.0 green:218/255.0 blue:218/255.0 alpha:1]; } else { cell.backgroundColor = [UIColor whiteColor]; } [cell setDataWithStr:[_infoArr objectAtIndex:indexPath.row]]; return cell; } } //MARK:- UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"選中了%@", [_infoArr[indexPath.row] objectForKey:@"routeName"]); }
6、這個方法就是實現(xiàn)上下滑動時候,左側(cè)和右側(cè)tableview聯(lián)動的實現(xiàn)方法。
//MARK:- UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == _titleTableView) { [_infoTableView setContentOffset:CGPointMake(_infoTableView.contentOffset.x, _titleTableView.contentOffset.y)]; } if (scrollView == _infoTableView) { [_titleTableView setContentOffset:CGPointMake(0, _infoTableView.contentOffset.y)]; } }
總結(jié)
啊,寫完感覺也是比較簡單,就是基本方法的配合使用,當(dāng)時想的時候也是沒有能一下想出來,還是自己基本功不好的原因吧。把這個效果的實現(xiàn)記錄在這里,也是為了提醒自己,也就是這個功能比較簡單,但是再怎樣的功能都是靠最基本的東西堆砌的。思想很重要,但是最重要的還是去實現(xiàn),光想沒有用,人不是靠嘴活的。與君共勉。
代碼地址:https://github.com/irembeu/HorizontalSwipListView.git
本地下載地址:http://xiazai.jb51.net/201706/yuanma/ListTableView(jb51.net).rar
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對給各位iOS開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS開發(fā)中視圖的下拉放大和上拉模糊的效果實現(xiàn)
這篇文章主要介紹了iOS開發(fā)中視圖的下拉放大和上拉模糊的效果實現(xiàn),本文以秘密App的效果作為示例進行講解,需要的朋友可以參考下2015-09-09iOS自定義UICollectionViewLayout實現(xiàn)瀑布流布局
這篇文章主要為大家詳細(xì)介紹了iOS自定義UICollectionViewLayout實現(xiàn)瀑布流布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12iOS開發(fā)之?dāng)?shù)字每隔3位用逗號分隔
以前在做電商app時經(jīng)常會針對稍大的金額展示出來,需要每隔千位添加逗號便于用戶識別,下面通過本文給大家分享ios中數(shù)字每隔3位用逗號分隔的實例代碼,需要的朋友參考下吧2017-09-09