ios 横向滚轮效果_iOS 可以纵向横向滑动的表格实现

背景

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

代码大体思路

由上面的GIF图和基本需求描述我们第一个想到的东西就是万能的tableview,没错,这个功能的完成当然离不开tableview,那么tableview应该怎样发挥它的功力呢,左右侧的信息需要对称,所以在这里我使用了两个tableview,也就是最左侧线路名称这一列是一个tableview,右侧的粉红色字体这些行是一个tableview。上下滑动两者关联是使用scrollview完成的。那接下来就结合代码简单说一下,也方便我以后回头看,哈哈哈。

代码解析

这是需要的原材料,每个变量都有注释它的功能了,一眼懂。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;

}

这是所需要的数据配置,我把里面所有需要的数据都放在数组李典里面了。我比较懒。哈哈哈哈- (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"}];

}

分步来看,首先是头部的,这个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 

CGFloat x = i * 100;

UILabel *label = [self quickCreateLabelWithLeft:x width:100 title:[[_infoArr objectAtIndex: i] objectForKey:@"title"]];

label.textAlignment = NSTextAlignmentCenter;

[_contentView addSubview:label];

}

}

那接下来就是配置最左侧那一栏和左侧粉红色字体那些行。也就这两个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];

}

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

}

这个方法就是实现上下滑动时候,左侧和右侧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)];

}

}

总结

啊,写完感觉也是比较简单,就是基本方法的配合使用,当时想的时候也是没有能一下想出来,还是自己基本功不好的原因吧。把这个效果的实现记录在这里,也是为了提醒自己,也就是这个功能比较简单,但是再怎样的功能都是靠最基本的东西堆砌的。思想很重要,但是最重要的还是去实现,光想没有用,人不是靠嘴活的。与君共勉。

额,这个是我打着盹写完的,如果您看到这里了,真的是特别感谢,有点儿困了,北京,晚安。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值