当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS实现可以纵向横向滑动的表格实例代码

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文主要给大家介绍了关于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)];
 }
}

总结

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

代码地址:https://github.com/irembeu/horizontalswiplistview.git

本地下载地址:

总结

以上就是这篇文章的全部内容了,希望本文的内容对给各位ios开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网