UITableView的一些方法
2015-11-11 20:59
405 查看
#import "RootViewController.h"
//签订tableView的协议
//两个协议 UITableViewDataSource, UITableViewDelegate
//DataSource(数据源)
@interface
RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
// UITableView
UITableView *tableView = [[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
// UITableViewStyleGrouped 分组 -默认在头部和尾部添加视图
// UITableViewStylePlain 默认
//*********************************
// 设置代理
// 协议方法
tableView.delegate =self;
// 数据源
tableView.dataSource =self;
//*********************************
//***********************************************************************
// 注册重用池
// 参数1
: 注册的是什么类型的cell
// 参数2
: 重用池的标识
[tableView registerClass:[UITableViewCellclass]
forCellReuseIdentifier:@"UITableCellIdentifier"];
//***********************************************************************
[self.viewaddSubview:tableView];
[tableViewrelease];
}
//返回多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return
4;
}
//分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return
10;
}
//如果不想显示每个section的footer ze高度返回0
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// 当直接返回0的时候系统会认为它是无效值
所以不会做任何改变所以返回float类型的最小值
d7f9
returnCGFLOAT_MIN;
}
//返回每个section头部的自定义视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *vv = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,30,10)];
vv.backgroundColor = [UIColorredColor];
return vv;
}
//返回每个section的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return
@"A";
}
#pragma mark -- 返回右侧的索引
//与section相互对应
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return@[@"1",@"2",
@"3",@"4"];
}
#pragma mark -- cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
// 取消选中状态
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
// 打印当前的indexPath.row
NSLog(@"%ld", indexPath.row);
}
#pragma mark -- 先执行行数
再每取一个cell都走一遍高
#pragma mark -- 返回每行高度的协议方法(默认高度为44.0)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath {
// indexPath -- 包含row(行)和section(区)用来定位是哪个cell
// 当行数为第一个的时候返回200的高度
if (indexPath.row ==0)
{
return
200;
}
return
80;
}
#pragma mark -- UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// tableView 以区(section)划分每个区包含若干行
// tableView 没有设置区个数的时候默认为1个区
// 返回10个row
if (section ==
0) {
return
5;
}else
if (section ==1) {
return
6;
}else
if (section ==2) {
return
7;
}else {
return
8;
}
return
20;
}
//每个row显示内容(cell)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"a"];
// 从重用池中获取cell,如果重用池中没有cell,系统会根据注册的cell自动创建并返回
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableCellIdentifier"];
//设置文字
cell.textLabel.text =@"迪丽热巴";
cell.detailTextLabel.text =@"ABCD";
cell.imageView.image = [UIImageimageNamed:@"Image"];
return cell;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//签订tableView的协议
//两个协议 UITableViewDataSource, UITableViewDelegate
//DataSource(数据源)
@interface
RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
// UITableView
UITableView *tableView = [[UITableViewalloc]
initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
// UITableViewStyleGrouped 分组 -默认在头部和尾部添加视图
// UITableViewStylePlain 默认
//*********************************
// 设置代理
// 协议方法
tableView.delegate =self;
// 数据源
tableView.dataSource =self;
//*********************************
//***********************************************************************
// 注册重用池
// 参数1
: 注册的是什么类型的cell
// 参数2
: 重用池的标识
[tableView registerClass:[UITableViewCellclass]
forCellReuseIdentifier:@"UITableCellIdentifier"];
//***********************************************************************
[self.viewaddSubview:tableView];
[tableViewrelease];
}
//返回多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return
4;
}
//分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return
10;
}
//如果不想显示每个section的footer ze高度返回0
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
// 当直接返回0的时候系统会认为它是无效值
所以不会做任何改变所以返回float类型的最小值
d7f9
returnCGFLOAT_MIN;
}
//返回每个section头部的自定义视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *vv = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,30,10)];
vv.backgroundColor = [UIColorredColor];
return vv;
}
//返回每个section的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return
@"A";
}
#pragma mark -- 返回右侧的索引
//与section相互对应
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return@[@"1",@"2",
@"3",@"4"];
}
#pragma mark -- cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath {
// 取消选中状态
[tableView deselectRowAtIndexPath:indexPathanimated:YES];
// 打印当前的indexPath.row
NSLog(@"%ld", indexPath.row);
}
#pragma mark -- 先执行行数
再每取一个cell都走一遍高
#pragma mark -- 返回每行高度的协议方法(默认高度为44.0)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath {
// indexPath -- 包含row(行)和section(区)用来定位是哪个cell
// 当行数为第一个的时候返回200的高度
if (indexPath.row ==0)
{
return
200;
}
return
80;
}
#pragma mark -- UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// tableView 以区(section)划分每个区包含若干行
// tableView 没有设置区个数的时候默认为1个区
// 返回10个row
if (section ==
0) {
return
5;
}else
if (section ==1) {
return
6;
}else
if (section ==2) {
return
7;
}else {
return
8;
}
return
20;
}
//每个row显示内容(cell)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"a"];
// 从重用池中获取cell,如果重用池中没有cell,系统会根据注册的cell自动创建并返回
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableCellIdentifier"];
//设置文字
cell.textLabel.text =@"迪丽热巴";
cell.detailTextLabel.text =@"ABCD";
cell.imageView.image = [UIImageimageNamed:@"Image"];
return cell;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音