资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

iOS如何实现可以纵向横向滑动的表格-创新互联

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

成都创新互联公司公司2013年成立,先为当阳等服务建站,当阳等地企业,进行企业商务咨询服务。为当阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

效果图

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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:iOS如何实现可以纵向横向滑动的表格-创新互联
转载来于:http://cdkjz.cn/article/hgode.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220