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基本使用方法总结1的更多相关文章

  1. iOS开发UITableView基本使用方法总结

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  2. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  3. 李洪强iOS开发Swift篇—10_方法

    李洪强iOS开发Swift篇—10_方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开 ...

  4. IOS开发中重写init方法使用需谨慎

    IOS开发中重写init方法使用需谨慎 今天在写一个小软件的时候出现一点问题,这个软件的功能是搜索全国学校,首页就是搜索输入框,在框中输入完要查询的学校所在省份,点击buttom后就会跳转到对应的视图 ...

  5. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  6. iOS开发使用半透明模糊效果方法整理

    虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包括今年最新发布的iOS8也沿袭了这一设计,甚至在OS X 10.10版Yosemite中也开 ...

  7. [转]iOS开发使用半透明模糊效果方法整理

    转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...

  8. iOS开发-获取属性和方法

    iOS开发数据存储有两种方式,属性列表和对象编码,属性列表可以通过NSArray,NSMutableArray,NSMutableDictionary,存储对象我们可以通过归档和解档来完成.如果我们想 ...

  9. iOS开发-UITableView自定义Cell

    UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...

随机推荐

  1. javascript实现KMP算法(没啥实用价值,只供学习)

    简单粗暴上代码 KMP的原理我就不讲了,想转过弯儿来不容易,建议大家先学会了怎么推导出next数组规律,然后准备两张纸,大纸上写上一行你要匹配的目标字符串,并分别写出位置编号,小纸上写上一行,也写上位 ...

  2. 【面试题043】n个骰子的点数

    [面试题043]n个骰子的点数 题目:     把n个骰子扔在地上,所有骰子朝上一面的点数之和为s, 输入n,打印出s的所有可能的值出现的概率.   n个骰子的总点数,最小为n,最大为6n,根据排列组 ...

  3. POJ1474 Video Surveillance(半平面交)

    求多边形核的存在性,过了这题但是过不了另一题的,不知道是模板的问题还是什么,但是这个模板还是可以过绝大部分的题的... #pragma warning(disable:4996) #include & ...

  4. HDU 1016 Prime Ring Problem (素数筛+DFS)

    题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...

  5. poj 1678 I Love this Game!

    思路:这题和博弈论的关系不大,主要是DP.记忆化搜索即可!!! 取的数一定是大于0的,所以将负数去掉!! 代码如下: #include<iostream> #include<cstd ...

  6. (8)nehe教程2-多边形

    参考自: http://www.yakergong.net/nehe/ 你的第一个多边形: 在第一个教程的基础上,我们添加了一个三角形和一个四边形.也许你认为这很简单,但你已经迈出了一大步,要知道任何 ...

  7. hdu 4159 Indomie (DP,数学概率)

    推出数学公式: #include<stdio.h> #include<string.h> __int64 C(int m,int n) { __int64 tmp=; if(m ...

  8. JSON 与JAVA对象之间的转换(转)

    JSON与XML两者目前在数据交互方面都有很高的使用率,随着现在SOA的兴起,异构系统的整合地位相应提高,本篇文章主要介绍JSON与JAVA对象之间的相互转换. 一.对普通类型的JSON模式的转换 一 ...

  9. Android 闹钟设置

    在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件.AlarmManager 提供了访问系统警报的服务,只要在程序 ...

  10. XML文件操作学习(一)

    受人启发,从今天开始也把学到的东西记在博客里加深印象,并且完成这个梳理过程. 最近大多数的时间都花费在做系统配置上了.大部分的配置比较复杂的都用xml文件来存储.暂时发现有以下几点需要注意的地方. 今 ...