iOS开发UI篇之tableView 的用法详解
2016-03-29 17:02
661 查看
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);
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; }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事