几乎大多数的IOS项目中都可以看得到UITableView 的影子,总结了一下,UITableView是iOS开发中,使用最广泛的组件之一,通常都用它来展示一列数据 。开始看一下UITableView的基本语法:
一、UITableView有两个代理协议
Protocol UITableViewDataSource:用来给TableView提供数据
Protocal UITableViewDelegate:控制TableView的展示方式以及事件响应
二、实现代理方法
1、UITableViewDataSource代理方法实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //指定有多少个分区(Section),默认为1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//指定每个分区中有多少行,默认为1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //绘制Cell
示例代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return1;//这里使用默认,如果你的数据需要分组显示,在这里就可以定义你所需要的组的个数
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrays count];//arrays是你所定义的数据存储数组
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier"; //相当于一个行标识
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
//tableViewCell重绘
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
NSUInteger row = indexPath.row; //获取行号
NSString *titleStr = [arrays objectAtIndex:row];//获取数据
cell.textLabel.text = titleStr;//数据显示
return cell;
}
当然这里还有一些复杂的使用,例如headerView 、 footerView 、titleForHeaderInSection 等等 。
-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section //设置分区高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath //改变行的高度
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath //行缩进
二、UITableViewDelegate的代理方法实现
UITableViewDelegate用来管理Row的选择和编辑,有四个方法如下:
tableView:willSelectRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
tableView:willDeselectRowAtIndexPath:
tableView:didDeselectRowAtIndexPath:
此四个方法管理Row的选择. 例如willSelectRowAtIndexPath, 如果此方法返回nil,那么所属的row将无法被选中。
tableView:willBeginEditingRowAtIndexPath:
tableView:didEndEditingRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath:
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
此四个方法在编辑Row时会被触发。editingStyleForRowAtIndexPath决定Row是否可以被编辑,删除或者移动。targetIndexPathForMoveFromRowAtIndexPath则在移动Row时会把触发,在交换Row位置的时候,必须同时交换DataSource中数据的位置。
示例代码:
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row];
return row;
}
//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
三、
常用的一些枚举类型选择
//选中cell时的颜色
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
TIPS:
自定义选中cell的背景颜色:
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
//cell右边按钮格式
typedef enum {
UITableViewCellAccessoryNone, // don't show any accessory view没有附件
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track 黑色向右的箭头
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks 蓝色附件按钮
UITableViewCellAccessoryCheckmark // checkmark. doesn't track 复选框,支持选择
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle
//改变换行线颜色
tableView.separatorColor = [UIColor blueColor];
//系统提供的UITableView也包含了四种风格的布局
typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;
分享到:
相关推荐
UITableView、UITableView基本用法、UITableView详解,UITabelView常用方法
UITableView基本用法大全
uitableview基本方法使用,代理方法、编辑、移动等。
UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件。 创建 首先创建一个新的项目,并添加一个MainViewController的Class文件 打开MainViewController.h文件 @interface ...
iOS开发中UITableview控件的基本使用及性能优化方法共15页.pdf.zip
UITableview控件基本使用 一、一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 代码如下: #import @interface NJHero : NSObject /** * 头像 */ @property (nonatomic, copy) NSString *icon; /** ...
本代码中介绍了uitableviewcontroller的基本用法,还有navigationcotrooler使用(涉及到逆向传值)
UITableViewController最基本用法实现(Accessory多选、自定义Style多选、目录表格、编辑表格) 用于初学者了解其UITableViewController
源码JHCellConfig,一种十分灵活易变的适用于创建有多种cell的UITableView的方法,不需要使用switch...case,在调整不同种cell的顺序、及增删某种cell时极其方便 -----本类相当于将tableView中cell所需的基本信息...
TableViewDataSource ...基本方法 典型的方法是在视图控制器中实现UITableViewDataSource 。 我们可以按以下部分组织内容: 标有城市图片和标题的标头; 欢迎消息和详细文本; 地点清单。 标头部分是静态的
UICollectionView IOS6新特性,类似于uitableview.基本方法解释。代码详细,带注释。
来源:github/waterflowViewLicence:MIT作者:|′ Mr—周√:face_blowing_a_kiss: ... 只要把代码拖进项目,导入头文件即可,创建控件,实现数据源方法即可,继承cell就可以了,用法和tableview相同,为cell设置重用标示符
1:masonry基本用法 2:UIScrollView布局 3:UITableView可变cell布局 4:UITableViewHeader和UITableViewFooter布局
DCTTableViewDataSources DCTTableViewDataSources是使对象用作UITableView的数据源的更结构化的方法。特征自动处理从笔尖或情节提要中创建的表格视图单元格可以与托管对象上下文和获取请求一起馈入的获取结果数据源...
在iOS开发中,开发”表单”界面,字段稍微多一点的一般都用UITableView来做,而XLForm就是这样一个框架,它是创建动态表格视图最牛逼的iOS库, 用它实现表单功能,非常简单,省心省力。但是很可惜,搜索了很多文章都...
基本集合(在某种程度上类似于“骨干集合”的方法) 此集合的Firebase适配器,以使其易于使用UITableView,UICollectionView等表示Firebase集合。Firebase适配器还提供对渐进式加载(按批N大小),完成块等的支持...
此外,您不必在UITableView的Delegate方法中添加其他代码,就可以像UITableViewCell一样自然使用它:) 演示项目中的视差效果 安装 首选的安装方式是通过 。 只需添加 pod 'MMParallaxCell' 然后运行pod install ...
例如商品列表、聊天列表时,我们需要通过某种操作来刷新列表,最常用的便是下拉刷新的方法了,下拉刷新作为iOS的标准控件,即使不实用第三方库也可以容易的实现,这篇文章将向大家讲解如何使用UIRefreshControl实现...
UIKit具有一个名为UITableView的类型,该类型使用三个函数来构造一个非常基本的表,该表在此项目中使用了这些函数: override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> ...