iOS开发UI篇之tableView 的用法详解

1.我们知道tableView是IOS中的高级视图,其继承与ScrollView,故我们知道他有具有ScrollView的所有功能。而且还扩展了许多。当然在这里就不一一介绍了。

2.tableView的表现格式分两种Plain和Grouped两种风格

3.tableView的两种代理类delegate和dataSource.这两种代理至关重要,我们在做tableView和这些代理是分不开的。

4.代理中比较常用的代理方法:

(1)dataSource的两个必须使用的代理

@required

//显示UITableView的Cell的个数

- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:(NSInteger )section

//Cell和model的数据的交互

- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

(2)delegate的常用代理方法

@ optional

//用于设定tableView的高度

- ( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

//当选中Cell时候调用的方法

- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

4.一般来说这四个代理已经能够处理函数tableView没有问题了,但是有时我们在做tableView时候,tableView的第一个Cell或者最后一个Cell和其他Cell不同,所以我们有可能用到下面的两个函数

//给tableview头的Cell和Model的数据交互

- ( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection:(NSInteger )section

//定义tableView的头部的高度

- ( CGFloat )tableView:( UITableView  *)tableView heightForHeaderInSection:(NSInteger )section

//给tableview尾部的Cell和Model的数据交互

- ( UIView *)tableView:( UITableView *)tableView viewForFooterInSection:( NSInteger)section

//定义tableView的尾部的高度

- ( CGFloat )tableView:( UITableView *)tableView heightForFooterInSection:(NSInteger )section

5.还有一种方法,应为tableView的对象自带的有这两个属性

例如:

ableView的对象自带的有这两个属性的属性要注意的是他接收的对象 UIView  

//截图

self .tableView.tableFooterView

self .tableView. tableHeaderView

以上基本上就可以做出完整的tableView了。那下面我们看一看tableView的扩展吧

6.我们知道tableView继承于scrollview,但是scrollView有时候加载过多的东西时候使用内存较大,会导致手机卡死;这时候我们一般有三个方法解决,一,重用scrollView  二,使用瀑布流 三,使用横向tableView

使用横向tableView:

一下例子只是我从工程中取出来的文件,可能运行不了,但是可以借鉴其步骤

其中代理函数不会发生改变,但是其中心和Frame都要发生改变

Frame需要X与Y 宽度和长度都要反过来

横向tableView主要有几句很重要的代码

这两句话:第一句话是将tableView横向旋转,第二句话是改变tableView中心。这两句话放在tabbleView的对象定义的时候,可见下面“设置界面”代码

table.transform = CGAffineTransformMakeRotation(- M_PI / 2);table.center = CGPointMake(self.view.frame.size.width / 2 , self.view.frame.size.height / 2 + 30);

这一句话是放在Cell和Model数据交互中,其作用是将Cell也横向旋转cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);

//设置界面
- (void) SettingsView
{
  self.tableViewX = [[UITableView alloc] initWithFrame:CGRectMake(100, 0, HEIGHT -100, WIDTH)];
  self.tableViewX.transform = CGAffineTransformMakeRotation(- M_PI / 2);
  self.tableViewX.center = CGPointMake(WIDTH / 2 , (HEIGHT + 100) /2 + 0);
  self.tableViewX.dataSource =self;
  self.tableViewX.delegate = self;
  self.tableViewX.pagingEnabled = YES;
  self.tableViewX.showsVerticalScrollIndicator= NO;
  self.tableViewX.bounces = YES;
  [self.view addSubview:self.tableViewX];
}
//显示有几个Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return 6;
}
//数据交互
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //数据
  NSArray * newpho = allNewsarray[0];
  NSArray * newarr = allNewsarray[1];
  NSDictionary * newsPhotosDic = newpho[indexPath.row];
  NSDictionary * newsdic = newarr[indexPath.row];
  NSArray * photos = [newsPhotosDic objectForKey:@"data"];
  NSString * identifier = [NSString stringWithFormat:@"Cell%lu",indexPath.row];
  YZXViewCellX * Cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  if (Cell == nil)
  {
    Cell = [[YZXViewCellX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier andArray:photos];
  }
  Cell.nowArray = [newsdic objectForKey:@"data"];
  Cell.PhotoArray = [newsPhotosDic objectForKey:@"data"];
  NSLog(@"+++++++++++++++%lu %lu",Cell.nowArray.count, Cell.PhotoArray.count);
  Cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);
  Cell.block = ^(YZXViewCellX * newCell, newsList * aa)
  {
    NSLog(@"详情");
    DetailedNewsController * det = [[DetailedNewsController alloc] init];
    det.New = aa;
    [self.navigationController pushViewController:det animated:YES];
  };
  return Cell;
}
//因为横向tableView代替scroll故,高度为屏幕宽度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return WIDTH;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值