本文为大家呈现了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

  1. //返回有多少个Sections
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  3. {
  4. return 1;
  5. }

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

这个方法返回   对应的section有多少个元素,也就是多少行。

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  2. {
  3. return 10;
  4. }

(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 的高度。

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
  4. UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
  5. if (cell == nil)
  6. {
  7. // Create a cell to display an ingredient.
  8. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  9. reuseIdentifier:showUserInfoCellIdentifier]
  10. autorelease];
  11. }
  12. // Configure the cell.
  13. cell.textLabel.text=@"签名";
  14. cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
  15. }

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

返回指定的 section 的header的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  2. {
  3. if (section ==0)
  4. return 80.0f;
  5. else
  6. return 30.0f;
  7. }

(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

返回指定的section 的 header  的 title,如果这个section header  有返回view,那么title就不起作用了。

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  2. {
  3. if (tableView == tableView_)
  4. {
  5. if (section == 0)
  6. {
  7. return @"title 1";
  8. }
  9. else if (section == 1)
  10. {
  11. return @"title 2";
  12. }
  13. else
  14. {
  15. return nil;
  16. }
  17. }
  18. else
  19. {
  20. return nil;
  21. }
  22. }

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

返回指定的 section header 的view,如果没有,这个函数可以不返回view

  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  2. {
  3. if (section == 0)
  4. {
  5. UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
  6. owner: self
  7. options: nil] lastObject];
  8. else
  9. {
  10. return nil;
  11. }
  12. }

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

当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。

  1. TableView.allowsSelection=YES;
  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;

如果不希望响应select,那么就可以用下面的代码设置属性:

  1. TableView.allowsSelection=NO;

下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. if (indexPath.section == 1)
  4. {
  5. return;
  6. }
  7. else if(indexPath.section==0)
  8. {
  9. switch (indexPath.row)
  10. {
  11. //聊天
  12. case 0:
  13. {
  14. [self onTalkToFriendBtn];
  15. }
  16. break;
  17. default:
  18. break;
  19. }
  20. }
  21. else
  22. {
  23. return ;
  24. }
  25. }

如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置

cell.selectionStyle = UITableViewCellSelectionStyleNone;

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. //cell被选中后的颜色不变
  3. cell.selectionStyle = UITableViewCellSelectionStyleNone;

(9)如何设置tableview  每行之间的分割线

  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone  即可。

(10)如何设置 tableview cell的背景颜色

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. //设置背景颜色
  3. 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 可以被编辑

首先要进入编辑模式:

  1. [TableView setEditing:YES animated:YES];

如果要退出编辑模式,肯定就是设置为NO

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

返回当前cell  要执行的是哪种编辑,下面的代码是返回删除模式

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. return UITableViewCellEditingStyleDelete;
  4. }

-(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。

  1. -(void) tableView:(UITableView *)aTableView
  2. commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
  3. forRowAtIndexPath:(NSIndexPath *)indexPath
  4. [chatArray removeObjectAtIndex:indexPath.row];
  5. [chatTableView reloadData];

(13)如何获得 某一行的CELL对象

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏的更多相关文章

  1. 哈希-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: ...

  2. 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏

    一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...

  3. 写在新建博客的第一天 分类: fool_tree的笔记本 2014-11-08 17:57 144人阅读 评论(0) 收藏

    来CSDN开博客的目的有两个: 其一是因为CSDN的代码输出,看过一些博文,觉得这里的代码输出真的很漂亮: 其二则是因为,感觉自己印象笔记用久了之后,渐渐地几乎不再自己写些东西了,习惯了方便的剪藏插件 ...

  4. 架构师速成7.3-devops为什么很重要 分类: 架构师速成 2015-07-07 17:22 410人阅读 评论(0) 收藏

    evops是一个很高大上的名字,其实说的简单点就是开发和运维本身就是一个团队的,要干就一起把事情干好.谁出了问题,网站都不行.作为一个架构师,必须要devops,而且要知道如何推行devops. 首先 ...

  5. 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 ...

  6. 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 ...

  7. refresh的停车场 分类: 栈和队列 2015-06-18 17:13 26人阅读 评论(0) 收藏

    refresh的停车场 TimeLimit: 1000ms Memory limit: 65536K 题目描述 refresh最近发了一笔横财,开了一家停车场.由于土地有限,停车场内停车数量有限,但是 ...

  8. 转自知乎,亲民好酒推荐 分类: fool_tree的笔记本 2014-11-08 17:37 652人阅读 评论(0) 收藏

    这里尽量为大家推荐一些符合大众喜好.业内公认好评."即使你不喜欢,你也会承认它不错"的酒款.而且介绍到的酒款还会有一个共同的特征,就是能让你方便的在网上买到. 大概会分为烈酒,利口 ...

  9. AndroidManifest.xml中的application中的name属性 分类: android 学习笔记 2015-07-17 16:51 116人阅读 评论(0) 收藏

    被这个不起眼的属性折磨了一天,终于解决了. 由于项目需要,要合并两个android应用,于是拷代码,拷布局文件,拷values,所有的都搞定之后程序还是频频崩溃,一直没有找到原因,学android时间 ...

随机推荐

  1. dom4j基本使用用法

        DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working ...

  2. AS3条件编译

    package { import flash.display.Sprite; public class Main extends Sprite { public function Main() { s ...

  3. Java学习之位运算符

    位运算符:&,|,^,~,<<,>> & (按位与):只有对应的两个二进制位均为1时,结果才为1.例如,9&5,即00001001&000001 ...

  4. 图片处理中的Dithering技术

    话说二战的时候,美国轰炸机每次执行任务,除了满载着威力强大的炸弹以外,还常常要装配一台计算机,飞机飞行方向和投弹的抛物线的计算都离不开这台机器.可是世界上第一台电子计算机在二战结束后才发明,轰炸机上当 ...

  5. 删除正在登录的SQL账号

    exec sp_who '用户名' kill @spid sp_droplogin 用户名

  6. 剑指offer反转链表

    way1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...

  7. myeclipse连接hadoop集群编程及问题解决

    原以为搭建一个本地编程测试hadoop程序的环境很简单,没想到还是做得焦头烂额,在此分享步骤和遇到的问题,希望大家顺利. 一.要实现连接hadoop集群并能够编码的目的需要做如下准备: 1.远程had ...

  8. usb免驱动摄像头实验

    1.编译openwrt系统内核使它支持usb,进入在/openwrt/trunk上执行make menuconfig 2.1). 添加USB 相关支持Kernel modules —> USB ...

  9. EL探索

  10. Python大神成长之路: 第一次学习记录

    一.Python发展史 二.Python2 or 3 博主选择了Python3. 从官网下载Python  www.python.org Windows安装python3.5.python2.7.安装 ...