UITableView基础 UITableView编辑,UITableViewCell自定义

每个格子是一个UITableViewCell,

cell的位置由row(行), section( 分区)决定

//注册cell
 [ self . tableView registerClass :[ MyCell class ] forCellReuseIdentifier : @"reuse" ];


//数据源协议和代理
tableView. dataSource = 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;


//协议中的方法
// 返回每行显示的 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
{
   
}

UITableView编辑

// 开启 tableview 的编辑模式
[
tableView  setEditing: YES  animated: YES ];


// 系统提供一个编辑按钮
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 ];
}


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 ];

//第三步
- ( void )layoutSubviews
{
    [
super   layoutSubviews ];
    
//cell 在这个方法中对所有的子视图重新布局
    
// cell 显示到 tableview 上之前   最后调用的一个方法
    
self . nameLable . frame  =  CGRectMake ( 0 0 self . contentView . frame . size . width / 2 self . contentView . frame . size . height );
}


PPT截图





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr__Hacker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值