温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS如何实现可以纵向横向滑动的表格

发布时间:2021-06-30 13:13:52 来源:亿速云 阅读:418 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关iOS如何实现可以纵向横向滑动的表格,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

效果图

iOS如何实现可以纵向横向滑动的表格

这个效果是今天公司项目里面遇上的,也是第一次遇见这种需求,所以记录下来,效果如上图。需求主要是可以实现上下的滑动,并且同时最左侧的“线路名称”这一列在向左滑动的时候是不能跟随滚动的。这个功能主要是实现用户可以方便查看关于一下难以看全的列表数据。下面说一下思路。

代码大体思路

由上面的GIF图和基本需求描述我们第一个想到的东西就是万能的tableview,没错,这个功能的完成当然离不开tableview,那么tableview应该怎样发挥它的功力呢,左右侧的信息需要对称,所以在这里我使用了两个tableview,也就是最左侧线路名称这一列是一个tableview,右侧的粉红色字体这些行是一个tableview。上下滑动两者关联是使用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循环创建出来的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创建的。

//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的代理方法实现。在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、这个方法就是实现上下滑动时候,左侧和右侧tableview联动的实现方法。

//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如何实现可以纵向横向滑动的表格”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios
AI