iOS如何實現(xiàn)可以縱向橫向滑動的表格-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關iOS如何實現(xiàn)可以縱向橫向滑動的表格,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司公司2013年成立,先為當陽等服務建站,當陽等地企業(yè),進行企業(yè)商務咨詢服務。為當陽企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

效果圖

iOS如何實現(xiàn)可以縱向橫向滑動的表格

這個效果是今天公司項目里面遇上的,也是第一次遇見這種需求,所以記錄下來,效果如上圖。需求主要是可以實現(xiàn)上下的滑動,并且同時最左側的“線路名稱”這一列在向左滑動的時候是不能跟隨滾動的。這個功能主要是實現(xiàn)用戶可以方便查看關于一下難以看全的列表數據。下面說一下思路。

代碼大體思路

由上面的GIF圖和基本需求描述我們第一個想到的東西就是萬能的tableview,沒錯,這個功能的完成當然離不開tableview,那么tableview應該怎樣發(fā)揮它的功力呢,左右側的信息需要對稱,所以在這里我使用了兩個tableview,也就是最左側線路名稱這一列是一個tableview,右側的粉紅色字體這些行是一個tableview。上下滑動兩者關聯(lián)是使用scrollview完成的。那接下來就結合代碼簡單說一下,也方便我以后回頭看,哈哈哈。

代碼解析

1、這是需要的原材料,每個變量都有注釋它的功能了,一眼懂。titleTableView是最左側的線路名稱這一列。infoTableView是粉紅色字體這些。contentView是titleTableView和最上方(除了“線路名稱”)這一列內容的superView。

@property (nonatomic, strong) UITableView *titleTableView;//標題TableView
@property (nonatomic, strong) UITableView *infoTableView;//內容TableView
@property (nonatomic, strong) UIScrollView *contentView;//內容容器
@property (nonatomic, strong) NSArray *infoArr;//數組

@end

@implementation ViewController {
 CGFloat _kOriginX;
 CGFloat _kScreenWidth;
 CGFloat _kScreenHeight;
}

2、這是所需要的數據配置,我把里面所有需要的數據都放在數組李典里面了。我比較懶。哈哈哈哈

- (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也就是預留的最左側的空間。最上面的一列使用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、那接下來就是配置最左側那一欄和左側粉紅色字體那些行。也就這兩個tableview創(chuàng)建的。

//MARK:- 詳細內容
- (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)上下滑動時候,左側和右側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)];
 }
}

關于“iOS如何實現(xiàn)可以縱向橫向滑動的表格”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前題目:iOS如何實現(xiàn)可以縱向橫向滑動的表格-創(chuàng)新互聯(lián)
網頁網址:http://www.muchs.cn/article14/hgode.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、響應式網站、建站公司、品牌網站設計域名注冊、用戶體驗

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網站建設