上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法。

一、属性

1、frame:设置控件的尺寸和大小

2、backgroundColor:设置控件的颜色

3、style:获取表视图的样式

4、dataSource:设置UITableViewDataSource代理

5、delegate:设置UITableViewDelegate代理

6、backgroundView:设置背景视图

7、editing:是否允许编辑,默认为NO

8、sectionHeaderHeight:设置组表视图的头标签高度

9、sectionFooterHeight:设置组表视图的尾标签高度

10、allowsSelection:在非编辑下,行是否可以选中,默认YES

11、allowsSelectionDuringEditing:控制某一行时,是否可以编辑,默认NO

12、allowsMultipleSelection:是否可以选择多行,默认NO

13、allowsMutableSelectionDuringEditing:在选择多行的情况下,是否可以编辑,默认NO

14、sectionIndexTrackingBackgroundColor:设置选中部分的背景颜色

15、sectionIndexMinimumDisplayRowCount:显示某个组索引列表在右边当行数达到这个值,默认是NSInteger的最大值

16、sectionIndexColor:选择某个部分的某行改变这一行上文本的颜色

17、separatorStyle:设置单元格分隔线的样式

18、separatorColor:设置选中单元格分隔线的颜色

19、tableHeaderView:设置组表的头标签视图

20、tableFooterView:设置组表的尾标签视图

二、常见方法

1、- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

tableview 有多少个section

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

     return ;

 }

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

对应的section有多少行

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

     return ;
}

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

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 6;
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return ;
} - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return ;
}

4、- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

返回指定的row 的cell,可以返回自定义的cell;

这里只看看最基本的用法:

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

     static NSString *Indentifier = @"UITableViewCell";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Indentifier];
} //移除所有子视图
[cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *view = (UIView*)obj;
[view removeFromSuperview];
}]; //添加新视图
UILabel *label = [[UILabel alloc]initWithFrame:(CGRect){,,,}];
label.text = [NSString stringWithFormat:@"第%d行",(int)indexPath.row];
[cell addSubview:label]; UIImageView *imageIcon = [[UIImageView alloc] initWithFrame:(CGRect){,,,}];
imageIcon.image = [UIImage imageNamed:@"u=3971024035,4095552302&fm=21&gp=0"];
[cell addSubview:imageIcon]; return cell; }

如图:

5、- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

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

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section==) {
return @"标题一";
}else
return @"标题二"; }

如图:

6、 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

同理设置FooterView:- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

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

注意:设置了headerView一定不要忘记返回高度!

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] initWithFrame:(CGRect){,,,}]; UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){,,,}];
title.text = @"headerView";
title.textColor = [UIColor orangeColor];
title.textAlignment = NSTextAlignmentCenter;
[headerView addSubview:title]; UIImageView *headImage = [[UIImageView alloc] initWithFrame:(CGRect){,,,}];
headImage.image =[UIImage imageNamed:@"u=1522827729,398642494&fm=21&gp=0"];
[headerView addSubview:headImage]; return headerView;
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return ;
}

如图:

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

当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableView.allowsSelection = YES 才行。

 - (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==) {
switch (indexPath.row) {
case :
//点击每行想实现什么动作写在这里就好了
break; default:
break;
}
}
}

下面附上完整代码:

 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{

     UITableView *tableView;
NSArray *dataArray; } @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //初始化,设置位置大小
tableView = [[UITableView alloc] initWithFrame:(CGRect){,,self.view.frame.size.width,self.view.frame.size.height}];
//添加到视图
[self.view addSubview:tableView];
//设置UITableViewDeledate代理
tableView.delegate = self;
//设置UITableViewDataSource代理
tableView.dataSource = self;
//设置单元格分隔线样式
tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.allowsSelection = YES; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Indentifier = @"UITableViewCell";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Indentifier];
} //移除所有子视图
[cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *view = (UIView*)obj;
[view removeFromSuperview];
}]; //添加新视图
UILabel *label = [[UILabel alloc]initWithFrame:(CGRect){,,,}];
label.text = [NSString stringWithFormat:@"第%d行",(int)indexPath.row];
[cell addSubview:label]; UIImageView *imageIcon = [[UIImageView alloc] initWithFrame:(CGRect){,,,}];
imageIcon.image = [UIImage imageNamed:@"u=3971024035,4095552302&fm=21&gp=0"];
[cell addSubview:imageIcon]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ; } //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// if (section==0) {
// return @"标题一";
// }else
// return @"标题二";
//
//} - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[UIView alloc] initWithFrame:(CGRect){,,,}]; UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){,,,}];
title.text = @"headerView";
title.textColor = [UIColor orangeColor];
title.textAlignment = NSTextAlignmentCenter;
[headerView addSubview:title]; UIImageView *headImage = [[UIImageView alloc] initWithFrame:(CGRect){,,,}];
headImage.image =[UIImage imageNamed:@"u=1522827729,398642494&fm=21&gp=0"];
[headerView addSubview:headImage]; return headerView;
} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return ;
} - (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==) {
switch (indexPath.row) {
case :
//点击每行想实现什么动作写在这里就好了
break; default:
break;
}
}
}

ios基础篇(十四)——UITableView(二)属性及基本用法的更多相关文章

  1. ios基础篇(四)——UILabel的常用属性及方法

    UILabel的常用属性及方法:1.text //设置和读取文本内容,默认为nil label.text = @”文本信息”; //设置内容 NSLog(@”%@”, label.text); //读 ...

  2. IOS基础之(十四) KVO/KVC

    资料参考: http://www.cnblogs.com/kenshincui/p/3871178.html http://www.cnblogs.com/stoic/archive/2012/07/ ...

  3. Bootstrap入门(二十四)data属性

    Bootstrap入门(二十四)data属性 你可以仅仅通过 data 属性 API 就能使用所有的 Bootstrap 插件,无需写一行 JavaScript 代码.这是 Bootstrap 中的一 ...

  4. python基础篇(四)

    PYTHON基础篇(四) 内置函数 A:基础数据相关(38) B:作用域相关(2) C:迭代器,生成器相关(3) D:反射相关(4) E:面向对象相关(9) F:其他(12) 匿名函数 A:匿名函数基 ...

  5. 小猪猪C++笔记基础篇(四)数组、指针、vector、迭代器

    小猪猪C++笔记基础篇(四) 关键词:数组,Vector. 一.数组与指针 数组相信大家学过C语言或者其他的语言都不陌生,简单的就是同一个变量类型的一组数据.例如:int a[10],意思就是从a开始 ...

  6. Java15-java语法基础(十四)抽象类

    Java15-java语法基础(十四)抽象类 一.抽象类的作用 三个类都有"执行任务"的方法,分别在这三个类中进行定义,因此需要重复编写代码,降低了程序开发效率,且增加了程序出现错 ...

  7. ios基础篇(二十六)—— UITableViewCell的分组索引与标记

    一.表视图的索引目录 首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇). 直接贴代码吧, #import "ViewController.h" @ ...

  8. linux基础-第十四单元 Linux网络原理及基础设置

    第十四单元 Linux网络原理及基础设置 三种网卡模式图 使用ifconfig命令来维护网络 ifconfig命令的功能 ifconfig命令的用法举例 使用ifup和ifdown命令启动和停止网卡 ...

  9. ios基础篇(五)——UITextField的详细使用

    UItextFieldField通常用于外部数据输入,以实现人机交互. 以下是UItextFieldField的属性及常见用法: 1.textField :设置文本框的默认文本. 2.Placehol ...

随机推荐

  1. Web3D编程总结——3D碰撞检测初探

    自己动手写一个方法比分析他人的写的方法困难很多,由此而来的对程序的进一步理解也是分析别人的代码很难得到的. 一.先来几张效果图: 1.场景中有两个半径为1的球体,蓝色线段从球心出发指向球体的“正向” ...

  2. mysql的一些基本操作语句

    -- 创建一个php2016的数据库create database php2016;-- 查看数据库的创建创建语句show create database php2016;-- 指定默认的操作数据库u ...

  3. 【Selenium】4.创建你的第一个Selenium IDE脚本

    http://newtours.demoaut.com/ 这个网站将会用来作为我们测试的网址. 通过录制来创建一个脚本 让我们来用最普遍的方法——录制来创建一个脚本.然后,我们将会用回放的功能来执行录 ...

  4. PeopleTools预警程序制作

    预警程序的概念:在主页上显示一个查询的结果.这个查询就是一个Record. 一.在Application Designer建一个项目,包含所有需要的Record. CUX_REC_BLRY Recor ...

  5. Windows便笺 快捷键

    便笺快捷键 便笺快捷键多数与Office相同 Ctrl+N 新建 Alt+F4 关闭便笺,下次打开内容还在 Ctrl+D 删除 Ctrl+E 居中 Ctrl+R 右对齐 Ctrl+L 左对齐 Ctrl ...

  6. Java面试题问与答——编译时与运行时

    在开发和设计的时候,我们需要考虑编译时,运行时以及构建时这三个概念.理解这几个概念可以更好地帮助你去了解一些基本的原理.下面是初学者晋级中级水平需要知道的一些问题. Q.下面的代码片段中,行A和行B所 ...

  7. printk函数日志级别的设置【转】

    本文转载自: 下面执行cat /proc/sys/kernel/printk 打印出的四个数字分别代表: 控制台日志级别.默认的消息日志级别.最低的控制台日志级别和默认的控制台日志级别 只有当prin ...

  8. notepad++代码自动补全功能

    可以代码自动补全功能,默认他是没有开启这个功能的,在首选项->备份与自动完成 里面有自动完成这一个设置,可以设置单词补全,也可以设置函数补全,这样写代码就快多了

  9. 开始跟踪Redis啦,开帖

    随着NoSql的流行,对这方面的产品开始关注起来,之前一直只是看看.从昨天开始决定把Redis的实现机制啃下来,毕竟代码量也就2W行. 每天花时间看看,记录下成果. here we go.

  10. Dynamics AX 2012 R2 为运行失败的批处理任务设置预警

    我们主要有两种类型的系统监视:环境健康监视和性能监视. 环境健康监视一般对系统性能影响非常小,是为了提醒潜在的问题. 性能监视通常更有侵入性.监视期间,添加一个负载到环境.因此,它可以回答特定的问题或 ...