iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结。首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource;然后 UITableView对象的 delegate要设置为 self;然后就可以实现这些delegate的一些方法拉。
UITableView基本使用方法
1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource
2.然后 UITableView对象的 delegate要设置为 self。
3.然后就可以实现这些delegate的一些方法拉。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回 tableview 有多少个section
- //返回有多少个Sections
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回 对应的section有多少个元素,也就是多少行。
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 10;
- }
(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
这个方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
这个方法返回指定的 section的footer view 的高度。
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
- UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
- if (cell == nil)
- {
- // Create a cell to display an ingredient.
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
- reuseIdentifier:showUserInfoCellIdentifier]
- autorelease];
- }
- // Configure the cell.
- cell.textLabel.text=@"签名";
- cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
- }
(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
返回指定的 section 的header的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- if (section ==0)
- return 80.0f;
- else
- return 30.0f;
- }
(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回指定的section 的 header 的 title,如果这个section header 有返回view,那么title就不起作用了。
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- if (tableView == tableView_)
- {
- if (section == 0)
- {
- return @"title 1";
- }
- else if (section == 1)
- {
- return @"title 2";
- }
- else
- {
- return nil;
- }
- }
- else
- {
- return nil;
- }
- }
(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
返回指定的 section header 的view,如果没有,这个函数可以不返回view
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
- if (section == 0)
- {
- UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
- owner: self
- options: nil] lastObject];
- else
- {
- return nil;
- }
- }
(8) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。
- TableView.allowsSelection=YES;
- cell.selectionStyle=UITableViewCellSelectionStyleBlue;
如果不希望响应select,那么就可以用下面的代码设置属性:
- TableView.allowsSelection=NO;
下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (indexPath.section == 1)
- {
- return;
- }
- else if(indexPath.section==0)
- {
- switch (indexPath.row)
- {
- //聊天
- case 0:
- {
- [self onTalkToFriendBtn];
- }
- break;
- default:
- break;
- }
- }
- else
- {
- return ;
- }
- }
如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置
cell.selectionStyle = UITableViewCellSelectionStyleNone;
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //cell被选中后的颜色不变
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }
(9)如何设置tableview 每行之间的分割线
- self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone 即可。
(10)如何设置 tableview cell的背景颜色
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //设置背景颜色
- cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
- }
(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
这个函数响应,用户点击cell 右边的 箭头(如果有的话)
(12)如何设置tableview 可以被编辑
首先要进入编辑模式:
- [TableView setEditing:YES animated:YES];
如果要退出编辑模式,肯定就是设置为NO
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
返回当前cell 要执行的是哪种编辑,下面的代码是返回删除模式
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleDelete;
- }
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。
- -(void) tableView:(UITableView *)aTableView
- commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
- forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [chatArray removeObjectAtIndex:indexPath.row];
- [chatTableView reloadData];
- }
(13)如何获得 某一行的CELL对象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
版权声明:本文为博主原创文章,未经博主允许不得转载。
iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏的更多相关文章
- 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...
- 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏
一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...
- 写在新建博客的第一天 分类: fool_tree的笔记本 2014-11-08 17:57 144人阅读 评论(0) 收藏
来CSDN开博客的目的有两个: 其一是因为CSDN的代码输出,看过一些博文,觉得这里的代码输出真的很漂亮: 其二则是因为,感觉自己印象笔记用久了之后,渐渐地几乎不再自己写些东西了,习惯了方便的剪藏插件 ...
- 架构师速成7.3-devops为什么很重要 分类: 架构师速成 2015-07-07 17:22 410人阅读 评论(0) 收藏
evops是一个很高大上的名字,其实说的简单点就是开发和运维本身就是一个团队的,要干就一起把事情干好.谁出了问题,网站都不行.作为一个架构师,必须要devops,而且要知道如何推行devops. 首先 ...
- Shuffle'm Up 分类: 函数 POJ 查找 2015-08-09 17:01 6人阅读 评论(0) 收藏
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7529 Accepted: 3466 Descript ...
- DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- refresh的停车场 分类: 栈和队列 2015-06-18 17:13 26人阅读 评论(0) 收藏
refresh的停车场 TimeLimit: 1000ms Memory limit: 65536K 题目描述 refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是 ...
- 转自知乎,亲民好酒推荐 分类: fool_tree的笔记本 2014-11-08 17:37 652人阅读 评论(0) 收藏
这里尽量为大家推荐一些符合大众喜好.业内公认好评."即使你不喜欢,你也会承认它不错"的酒款.而且介绍到的酒款还会有一个共同的特征,就是能让你方便的在网上买到. 大概会分为烈酒,利口 ...
- AndroidManifest.xml中的application中的name属性 分类: android 学习笔记 2015-07-17 16:51 116人阅读 评论(0) 收藏
被这个不起眼的属性折磨了一天,终于解决了. 由于项目需要,要合并两个android应用,于是拷代码,拷布局文件,拷values,所有的都搞定之后程序还是频频崩溃,一直没有找到原因,学android时间 ...
随机推荐
- Mesos架构
Mesos Architecture 上图显示了 Mesos 的主要组成部分. Mesos 由一个 master daemon 来管理 slave daemon 在每个集群节点上的运行, mesos ...
- 转:Loadrunner报错“Too many local variablesAction.c”解决方法
问题描述,在Action.c里定义数组时如果数组长度过长,如char a[1024*1024]运行时即会报错: 问题原因及解决方法如下: 1. VuGen对于局部变量可以分配的最大内存为64K,如果想 ...
- C#中:函数访问级别对函数形参访问级别的约束
Inconsistent accessibility: parameter type 'Program.CommandLineInfo' is less accessible than method ...
- FTP 7.5 自定义扩展功能
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- php basename()文件夹 路径 文件后缀名 读取pathinfo()
$path = "/www/mywebsite/images/myphoto.jpg"; 1.pathinfo()函数 pathinfo()函数返回的是一个包含了文件信息的数组,数 ...
- zencart url特殊字符处理
1. 支持 在后台的seo url 将Outputw3c 改为false 2.删除特殊字符 这对于在少量的zen cart网站上处理少量的特殊字符可能还适用,实际上我们经常在导入产品数据时或者或少会带 ...
- MyEclipse8.5优化经验
第一步: 取消自动validation validation有一堆,什么xml.jsp.jsf.js等等,我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: ...
- Ubuntu vim显示行号语法高亮自动缩进
配置文件名为Ubuntu vimrc在Fedora中vim的配置文件存放在/etc目录中,配置文件名为Ubuntu vimrc在终端 输入以下命令来编辑Ubuntu vimrc配置文件:sudo vi ...
- activity管理类 appManager
程序启动第一个界面类: net.oschina.app.AppStart功能描述:一张图片代码细节描述:一个透明度的动画效果,效果动画完成后自动启动新的Activity(Main) 基本BaseAct ...
- SQL2005附加数据库时遇到的问题:用户组或角色在当前数据库已存在
一次 附加备份数据库的 mdf 文件 成功后 创建登陆用户 但是 无法映射该用户的 对应数据库 出现 用户组或角色在当前数据库已存在 的问题 首先介绍一下sql server中“ ...