iOS实现可以纵向横向滑动的表格实例代码

2020-01-20 23:01:12于丽

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"]);
}