表视图 UITableView,iOS中最重要的视图,随处可⻅见。 表视图通常⽤用来管理⼀一组具有相同数据结构的数据。

UITableView继承⾃自UIScrollView,所以可以滚动,表视图的每⼀一条数据都是显⽰示在UITableViewCell对象中,表视图可以分区显⽰示数据,每个分区称为⼀一个section,每⼀一⾏行称为 row,编号都是从0开始

表视图的创建(重要属性)

style样式:plain、group
分隔线样式:separatorStyle
分隔线颜色:separateColor
行高: rowheight
去掉点击后产生的灰色背景色:cell.selectionStyle=UITableViewCellSelectionStyleNone;

DataSource数据源

我们需要给tableView指定⼀一个数据源,它负责给tableView提供数据 需要实现协议中两个必须实现的⽅方法

- (NSInteger)tableView:(UITableView *)tableView

我们需要给tableView指定⼀一个数据源,它负责给tableView提供数据

需要实现协议中两个必须实现的⽅方法:

-numberOfRowsInSection:(NSInteger)section;

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

UITableView中每⼀一个单元格,被称为⼀一个cell

(UITableViewCell)。 系统预置了4种(枚举)样式的cell。 不同样式的cell包含的控件有细微差别。

 
设置图片:imageView
设置文本:textLable
指定选中效果:selectionStyle
指定辅助效果样式:accessoryType

表视图的重用机制

UITableView靠mutableSet来实现重⽤用功能

出屏幕的cell会被添加到mutableSet中,进⼊入屏幕的cell,先从set中 获取,如果获取不到,才创建⼀一个cell。在cell显⽰示之前,给cell赋上相应的内容。

cell的reuseIdentifier是重⽤用的关键。

表视图的配置

NSIndesPath: row 、section

+(NSIndesPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;

多个分区

tableView默认是⼀一个分区,可以设置多个分区 tableView的plain、group样式决定分区的样式不同

每个分区可以设置区头区尾

tableView默认是⼀一个分区,可以设置多个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView; //分区数

- (NSString *)tableView:(UITableView *)tableView

tableView的plain、group样式决定分区的样式不同

titleForHeaderInSection:(NSInteger)section; //分区头标题

 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; //右侧竖排索引

自定义头尾


Delegate

- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section;

自定义头尾

单元格的高度与选中

Delegate

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath

单元格的高度与选中

参考1  参考2  参考3

 
代码:
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = ; // 设置tableView的分割线的样式
// self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;//不是很明显的线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//比较明显的线条
// self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//没有分割线 //设置分割线的背景颜色
self.tableView.separatorColor = [UIColor redColor];
//设置分割线的左右距离
self.tableView.separatorInset = UIEdgeInsetsMake(, , , );
//分割线的宽度 ??? /**
下面的两个:一个表头,一个表尾都是与分组没有任何关系的,只与tableView 的顶部与下部有关系
*/
//设置一下表头的视图,一般用来做一个轮播图片,这个图片要在表头的上面
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
view.backgroundColor = [UIColor blueColor];
self.tableView.tableHeaderView = view; //设置一下表尾的视图,一般用于上拉刷新,这个要在表尾标题的下面
UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
view2.backgroundColor = [UIColor redColor];
self.tableView.tableFooterView = view2; } //设置表的表头的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"我是表头标题";
}
//设置表的表尾巴的标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"我是表的表尾巴";
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
//添加的cell右边的 button
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd]; //添加cell右边的 按钮
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //添加 cell右边的对号,用于提示用户完成一定的操作
// cell.accessoryType = UITableViewCellAccessoryCheckmark; //添加 cell 右边的 按钮 + 箭头
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; //添加cell 一个 switch 控件
// UISwitch * swit = [[UISwitch alloc] init];
// cell.accessoryView = swit;
// [swit addTarget:self action:@selector(valuechange:) forControlEvents:UIControlEventValueChanged]; /*
//设置cell 的背景颜色
UIImage * image = [UIImage imageNamed:@"img_01.png"];
UIImage * image2 = [UIImage imageNamed:@"img_02.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:image];//没有选中
//选中
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:image2];
*/ cell.textLabel.text = @"你舅舅家";
return cell;
}
-(void)valuechange:(UISwitch *)swit{
NSLog(@"选择改变了");
} //专门为accessoryType服务,对自定义控件不响应
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"我是cell 右边的按钮");
} //取消选中行的事件
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"取消点击事件");
}
//点中某一cell 的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"确实选中了某一行");
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ;
} @end

tableView 应用demo

UI:UITableView表视图的更多相关文章

  1. UI学习笔记---第十天UITableView表视图编辑

    UITableView表视图编辑 表视图编辑的使用场景 当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话 当我们需要手 ...

  2. UI学习笔记---第九天UITableView表视图

    UITableView表视图 一.表视图的使用场景 表视图UITableView是iOS中最重要的视图,随处可见,通常用来管理一组具有相同数据结构的数据 表视图继承自UIScrollView,所以可以 ...

  3. UITableView表视图以及重建机制

    表视图UITableView   表视图UITableView,是IOS中最重要的视图,随处可见 表视图通常用来管理一组具有相同数据结构的数据 UITableView继承自UIScrollView,所 ...

  4. UI学习笔记---第十一天UITableView表视图高级-自定义cell

    自定义cell,多类型cell混合使用,cell自适应高度 自定义cell就是创建一个UITableViewCell的子类 把cell上的空间创建都封装在子类中,简化viewController中的代 ...

  5. UI基础:UITableView表视图

    表视图 UITableView,iOS中最重要的视图,随处可见. 表视图通常用来管理一组具有相同数据结构的数据. UITableView继承于UIScrollView,所以可以滚动 表视图的每条数据都 ...

  6. UITableView 表视图编辑

    UITableViewController(表视图控制器)继承自UIViewController,自带一个tableView self.view不是UIView而是UITableView dataso ...

  7. iOS UITableView表视图滚动隐藏UINavigationController导航栏

    UITableView 继承于UIScrollView 所以UIScrollView 的代理方法相同适用于UITableView 中 隐藏导航栏的方法为: self.navigationControl ...

  8. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  9. IOS 表视图UITableView 束NSBundle

    今天搞了一下表视图UITableView 表视图是在以后应用程序开发中经常用到的一个视图,所以必须要熟练掌握 所获不多,对视图有了一个大概的了解 其中有用到NSBundle , 束   这个类 先说一 ...

随机推荐

  1. Linux服务器偶尔无法访问问题

    最近上了一台web服务器(本地包含mysql服务器),在运行一段时间发现服务器偶尔会无法访问, 包括mysql,ftp以及ssh等都无法响应,但是已经连接上的ssh不受任何影响,在查看系统log时, ...

  2. Error accessing PRODUCT_USER_PROFILE

    1.问题现象再现1)创建用户secSQL> create user sec identified by sec; User created. 2)授权SQL> grant connect, ...

  3. 一条sql导致数据库整体性能下降的诊断和解决的全过程

    今天早上一来,数据库load就比往常高了许多.想想数据库唯一的变化是昨天早上我曾经重新分析过数据库对象. [@more@] 发现数据库load很高,首先看top发现没有特别异常的进程,在数据库中适时抓 ...

  4. Android 动画 6问6答

    1.view 动画有哪些需要注意的? 答:view动画 本身比较简单.http://www.cnblogs.com/punkisnotdead/p/5179115.html 看这篇文章的第五问就可以了 ...

  5. [Everyday Mathematics]20150218

    设 $A,B$ 是 $n$ 阶复方阵, 适合 $$\bex A^2B+BA^2=2ABA. \eex$$ 试证: 存在 $k\in\bbZ^+$, 使得 $(AB-BA)^k=0$.

  6. DOS攻击和DDOS攻击有啥区别啊

    DDOS是DOS攻击中的一种方法. DoS:是Denial of Service的简称,即拒绝服务,不是DOS操作系统,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务. ...

  7. 【LeetCode 239】Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  8. Python的descriptor (2)

    前面说了descriptor,这个东西其实和Java的setter,getter有点像.但这个descriptor和上文中我们开始提到的函数方法这些东西有什么关系呢? 所有的函数都可以是descrip ...

  9. Chapter12:动态内存

    智能指针——shared_ptr 为了更容易地使用动态内存,新的标准提供了智能指针来管理动态对象.智能指针的行为类似常规指针,重要的区别是它负责自动释放指向的对象. 智能指针的使用方式与普通指针类似. ...

  10. 使用Java进行MD5加密

    使用Java自带的MessageDigest类可以轻松实现MD5加密,只不过加密后得到的是byte数组,我们需要将其转换为16进制的字符. 代码如下: package com.stepsoft.tes ...