每个格子是一个UITableViewCell,
cell的位置由row(行), section( 分区)决定
//注册cell
[
self
.
tableView
registerClass
:[
MyCell
class
]
forCellReuseIdentifier
:
@"reuse"
];
//数据源协议和代理
tableView.
dataSource
=
self
;
tableView. delegate = self ;
// 分割线的颜色
tableView. delegate = self ;
// 分割线的颜色
tableView.separatorColor
= [
UIColor
redColor
];
tableView.separatorStyle //设置tableView的那条线
//
统一设置
cell
的高度
tableView. rowHeight = 50 ;
// 给 tableview 添加一个顶部 view
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 100 , 200 )];
view. backgroundColor = [ UIColor grayColor ];
tableView. tableHeaderView = view;
tableView. rowHeight = 50 ;
// 给 tableview 添加一个顶部 view
UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 100 , 200 )];
view. backgroundColor = [ UIColor grayColor ];
tableView. tableHeaderView = view;
//协议中的方法
//
返回每行显示的
cell
的内容
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
//cell 的重用机制
// 每当一个 tableview 要显示一个 cell 的时候,系统都会调用这个方法给 tableview 提供一个新 cell
// 每个 tableview 内部都有若干个 cell 的重用池,每当需要 cell 的时候,都去某个重用池中取得一个 cell
// 如果重用池中有 cell , 就直接使用;如果没有,就创建一个新 cell ,给 cell 一个重用标志, 便于系统区分。
//1. 从重用池中尝试获取一个 cell
static NSString *str = @"reuse" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :str];
//2. 判断是否取得一个 cell
if (cell == nil ) {
// 如果取得的 cell 是 nil ,就创建一个新的 cell
cell = [[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier :str] autorelease ];
NSLog ( @" 需要新的 cell" );
}
NSString *name = [ self . arr objectAtIndex :indexPath. row ];
//3. 对 cell 重新赋值 , 使用
cell. textLabel . text = name;
cell. detailTextLabel . text = [ NSString stringWithFormat : @"section : %d row : %d" , indexPath. section , indexPath. row ];
return cell;
}
// 告诉 tableview 每个分区( section )显示多少行( row )
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
// 提供数据的数组的行数
return [ self . arr count ];
}
// 返回多个分区
- ( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView
{
return 10 ;
}
// 设置分区的顶部显示的文字
- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{
return [ NSString stringWithFormat : @"section : %d" , section];
}
// 设置分区顶部的高度
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section
{
return 60 ;
}
// 自定义一个分区的顶部 view
- ( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection:( NSInteger )section
{
UIView * view = [[ UIView alloc ] init ];
view. backgroundColor = [ UIColor yellowColor ];
return [view autorelease ];
}
// 处理点击 cell 的事件
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath
{
NSLog ( @"%@" , [ self . arr objectAtIndex :indexPath. row ]);
}
// 取消选中时触发的方法
- ( void )tableView:( UITableView *)tableView didDeselectRowAtIndexPath:( NSIndexPath *)indexPath
{
}
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
//cell 的重用机制
// 每当一个 tableview 要显示一个 cell 的时候,系统都会调用这个方法给 tableview 提供一个新 cell
// 每个 tableview 内部都有若干个 cell 的重用池,每当需要 cell 的时候,都去某个重用池中取得一个 cell
// 如果重用池中有 cell , 就直接使用;如果没有,就创建一个新 cell ,给 cell 一个重用标志, 便于系统区分。
//1. 从重用池中尝试获取一个 cell
static NSString *str = @"reuse" ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :str];
//2. 判断是否取得一个 cell
if (cell == nil ) {
// 如果取得的 cell 是 nil ,就创建一个新的 cell
cell = [[[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier :str] autorelease ];
NSLog ( @" 需要新的 cell" );
}
NSString *name = [ self . arr objectAtIndex :indexPath. row ];
//3. 对 cell 重新赋值 , 使用
cell. textLabel . text = name;
cell. detailTextLabel . text = [ NSString stringWithFormat : @"section : %d row : %d" , indexPath. section , indexPath. row ];
return cell;
}
// 告诉 tableview 每个分区( section )显示多少行( row )
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
// 提供数据的数组的行数
return [ self . arr count ];
}
// 返回多个分区
- ( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView
{
return 10 ;
}
// 设置分区的顶部显示的文字
- ( NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{
return [ NSString stringWithFormat : @"section : %d" , section];
}
// 设置分区顶部的高度
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section
{
return 60 ;
}
// 自定义一个分区的顶部 view
- ( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection:( NSInteger )section
{
UIView * view = [[ UIView alloc ] init ];
view. backgroundColor = [ UIColor yellowColor ];
return [view autorelease ];
}
// 处理点击 cell 的事件
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath
{
NSLog ( @"%@" , [ self . arr objectAtIndex :indexPath. row ]);
}
// 取消选中时触发的方法
- ( void )tableView:( UITableView *)tableView didDeselectRowAtIndexPath:( NSIndexPath *)indexPath
{
}
UITableView编辑
//
开启
tableview
的编辑模式
[ tableView setEditing: YES animated: YES ];
[ tableView setEditing: YES animated: YES ];
//
系统提供一个编辑按钮
self . navigationItem . rightBarButtonItem = self . editButtonItem ;
self . navigationItem . rightBarButtonItem = self . editButtonItem ;
//
点击删除触发的方法
- ( void ) tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath *)indexPath
{
// 对进行的操作的判断(添加/删除)
if (editingStyle == UITableViewCellEditingStyleDelete ) {
// 写代码删除相应的 cell
// 在删除 cell 之前,一定要删除数据源里面相应的内容
[ self . arr removeObjectAtIndex :indexPath. row ];
NSArray *arr = [ NSArray arrayWithObjects :indexPath, nil ];
// 参数 1: 要删除的 indexpath 组成的数组
// 参数 2: 删除时展现的动画动画效果
[tableView deleteRowsAtIndexPaths :arr withRowAnimation : UITableViewRowAnimationLeft ];
} else if (editingStyle == UITableViewCellEditingStyleInsert ){
// [self.arr addObject:<#(id)#>]
}
}
// 点击编辑按钮 系统会自动调用这个方法
- ( void )setEditing:( BOOL )editing animated:( BOOL )animated
{
[ super setEditing :editing animated :animated];
// 利用系统的编辑按钮 改变 tableview 的编辑状态
[ self . tableView setEditing :editing animated : YES ];
}
// 改变 cell 的编辑样式
- ( UITableViewCellEditingStyle )tableView:( UITableView *)tableView editingStyleForRowAtIndexPath:( NSIndexPath *)indexPath
{
// if (indexPath.row == 0) {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert ;
}
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
// // 是否能够被编辑
// return NO;
//}
// 移动数据调用的方法
- ( void )tableView:( UITableView *)tableView moveRowAtIndexPath:( NSIndexPath *)sourceIndexPath toIndexPath:( NSIndexPath *)destinationIndexPath
{
NSLog ( @"%d %d" , sourceIndexPath. row , destinationIndexPath. row );
// 获取要移动的数据
id str = [[ self . arr objectAtIndex :sourceIndexPath. row ] retain ];
// 将数据从原来的位置移除掉
[ self . arr removeObjectAtIndex :sourceIndexPath. row ];
// 把数据放在目的位置上
[ self . arr insertObject :str atIndex :destinationIndexPath. row ];
[str release ];
}
- ( void ) tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath *)indexPath
{
// 对进行的操作的判断(添加/删除)
if (editingStyle == UITableViewCellEditingStyleDelete ) {
// 写代码删除相应的 cell
// 在删除 cell 之前,一定要删除数据源里面相应的内容
[ self . arr removeObjectAtIndex :indexPath. row ];
NSArray *arr = [ NSArray arrayWithObjects :indexPath, nil ];
// 参数 1: 要删除的 indexpath 组成的数组
// 参数 2: 删除时展现的动画动画效果
[tableView deleteRowsAtIndexPaths :arr withRowAnimation : UITableViewRowAnimationLeft ];
} else if (editingStyle == UITableViewCellEditingStyleInsert ){
// [self.arr addObject:<#(id)#>]
}
}
// 点击编辑按钮 系统会自动调用这个方法
- ( void )setEditing:( BOOL )editing animated:( BOOL )animated
{
[ super setEditing :editing animated :animated];
// 利用系统的编辑按钮 改变 tableview 的编辑状态
[ self . tableView setEditing :editing animated : YES ];
}
// 改变 cell 的编辑样式
- ( UITableViewCellEditingStyle )tableView:( UITableView *)tableView editingStyleForRowAtIndexPath:( NSIndexPath *)indexPath
{
// if (indexPath.row == 0) {
// return UITableViewCellEditingStyleDelete;
// }
return UITableViewCellEditingStyleInsert ;
}
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
// // 是否能够被编辑
// return NO;
//}
// 移动数据调用的方法
- ( void )tableView:( UITableView *)tableView moveRowAtIndexPath:( NSIndexPath *)sourceIndexPath toIndexPath:( NSIndexPath *)destinationIndexPath
{
NSLog ( @"%d %d" , sourceIndexPath. row , destinationIndexPath. row );
// 获取要移动的数据
id str = [[ self . arr objectAtIndex :sourceIndexPath. row ] retain ];
// 将数据从原来的位置移除掉
[ self . arr removeObjectAtIndex :sourceIndexPath. row ];
// 把数据放在目的位置上
[ self . arr insertObject :str atIndex :destinationIndexPath. row ];
[str release ];
}
UITableViewCell自定义
//第一步
//
创建自己的两个视图属性,属性名不要和系统的属性重名(
imageview
,
textlable
,
detailTextLabel
)
@property ( nonatomic , retain ) UILabel *nameLable;
//第二步
//
初始化方法
初始化自己的视图对象,
进行简单的设置 不设置fram
self
.
nameLable
= [[
UILabel
alloc
]
init
];
self . nameLable . backgroundColor = [ UIColor magentaColor ];
[ self . contentView addSubview : self . nameLable ];
[ _nameLable release ];
self . nameLable . backgroundColor = [ UIColor magentaColor ];
[ self . contentView addSubview : self . nameLable ];
[ _nameLable release ];
//第三步
- (
void
)layoutSubviews
{
[ super layoutSubviews ];
//cell 在这个方法中对所有的子视图重新布局
// 在 cell 显示到 tableview 上之前 最后调用的一个方法
self . nameLable . frame = CGRectMake ( 0 , 0 , self . contentView . frame . size . width / 2 , self . contentView . frame . size . height );
}
{
[ super layoutSubviews ];
//cell 在这个方法中对所有的子视图重新布局
// 在 cell 显示到 tableview 上之前 最后调用的一个方法
self . nameLable . frame = CGRectMake ( 0 , 0 , self . contentView . frame . size . width / 2 , self . contentView . frame . size . height );
}
PPT截图