這篇文章將為大家詳細(xì)講解有關(guān)iOS如何實(shí)現(xiàn)可以縱向橫向滑動(dòng)的表格,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括長沙縣網(wǎng)站建設(shè)、長沙縣網(wǎng)站制作、長沙縣網(wǎng)頁制作以及長沙縣網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,長沙縣網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到長沙縣省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
效果圖
這個(gè)效果是今天公司項(xiàng)目里面遇上的,也是第一次遇見這種需求,所以記錄下來,效果如上圖。需求主要是可以實(shí)現(xiàn)上下的滑動(dòng),并且同時(shí)最左側(cè)的“線路名稱”這一列在向左滑動(dòng)的時(shí)候是不能跟隨滾動(dòng)的。這個(gè)功能主要是實(shí)現(xiàn)用戶可以方便查看關(guān)于一下難以看全的列表數(shù)據(jù)。下面說一下思路。
代碼大體思路
由上面的GIF圖和基本需求描述我們第一個(gè)想到的東西就是萬能的tableview,沒錯(cuò),這個(gè)功能的完成當(dāng)然離不開tableview,那么tableview應(yīng)該怎樣發(fā)揮它的功力呢,左右側(cè)的信息需要對(duì)稱,所以在這里我使用了兩個(gè)tableview,也就是最左側(cè)線路名稱這一列是一個(gè)tableview,右側(cè)的粉紅色字體這些行是一個(gè)tableview。上下滑動(dòng)兩者關(guān)聯(lián)是使用scrollview完成的。那接下來就結(jié)合代碼簡(jiǎn)單說一下,也方便我以后回頭看,哈哈哈。
代碼解析
1、這是需要的原材料,每個(gè)變量都有注釋它的功能了,一眼懂。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":@"出團(tuán)日期", @"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":@"價(jià)格", @"routeName":@"線路名稱三", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"}, @{@"title":@"團(tuán)代號(hào)", @"routeName":@"線路名稱四", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}]; }
3、分步來看,首先是頭部的,這個(gè)titleLabel是最左上角的“線路名稱”這四個(gè)字,contentView的配置,上面說了這個(gè)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è)粉紅色字體那些行。也就這兩個(gè)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的代理方法實(shí)現(xiàn)。在cellForRowAtIndexPath這個(gè)代理方法中,將兩個(gè)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、這個(gè)方法就是實(shí)現(xiàn)上下滑動(dòng)時(shí)候,左側(cè)和右側(cè)tableview聯(lián)動(dòng)的實(shí)現(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)]; } }
關(guān)于“iOS如何實(shí)現(xiàn)可以縱向橫向滑動(dòng)的表格”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
新聞名稱:iOS如何實(shí)現(xiàn)可以縱向橫向滑動(dòng)的表格
文章分享:http://chinadenli.net/article4/gohcie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站設(shè)計(jì)、營銷型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、定制網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)