UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)

一、tableView的编辑
tableView 编辑的步骤 :
 
 *  1.
tableView成为可编辑状态
 
    编辑按钮触发方法
 
 *  -1.
激活编辑状态

- ( void )rightButton:( UIBarButtonItem *)rightButton
{
  
// 开启 UITableView 编辑状态
   
// self.tableView.editing 默认是 NO
    [
self . tableView setEditing :! self . tableView . editing animated : YES ];
   
  
// 更改按钮的标题
   
if ( _tableView . editing ) {
        rightButton.
title = @" 完成 " ;
    }
else {
        rightButton.
title = @" 编辑 " ;
    }
}
 *  2. 返回(指定)可以被编辑的分区的行  (默认是YES)
  // 如果不写的话 默认是 YES
- (
BOOL )tableView:( UITableView *)tableView canEditRowAtIndexPath:( NSIndexPath *)indexPath
{
  
   
return YES ;
   
}
 *  3. 返回(指定)可以被编辑的分区的行的样式  添加 Or 删除
  // 返回哪个分区的哪行的 编辑样式
- (
UITableViewCellEditingStyle )tableView:( UITableView *)tableView editingStyleForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
// 先判断分区
   
if (indexPath. section == 0 ) {
       
// 再判断数据
       
if ([ self . firstDataArray [indexPath. row ] isEqualToString : @" 添加 " ]) {
           
return UITableViewCellEditingStyleInsert ;
        }
    }
else if (indexPath. section == 1 ) {
       
// 再判断数据
       
if ([ self . secondDataArray [indexPath. row ] isEqualToString : @" 添加 " ]) {
           
return UITableViewCellEditingStyleInsert ;
        }
    }
   
return UITableViewCellEditingStyleDelete ;
}
 *  4. 按照编辑的样式 来提交编辑的结果,完成不同的操作
 
        -1-  √
更新数据源数据
        -2-  √
刷新界面
int i = 4 , j = 4 ;
// 4. 按照编辑的样式 提交哪个分区哪行的结果 完成编辑
- (
void )tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle )editingStyle forRowAtIndexPath:( NSIndexPath *)indexPath
{
   
   
NSString *str = [ NSString stringWithFormat : @"%d" , i ];
   
NSString *str1 = [ NSString stringWithFormat : @"%d" , j ];

   
// 更新数据
   
   
// 刷新界面
   
   
// 先判断分区
   
if (indexPath. section == 0 ) {
       
// 第一分区
           
// 再判断编辑样式
       
if (editingStyle == UITableViewCellEditingStyleDelete ) {
           
// 删除
               
// 利用 indexPath.row 删除数组中相对应的数据
            [
self . firstDataArray removeObjectAtIndex :indexPath. row ];
           
// 刷新界面
               
// 下面方法 用在删除数据的时候刷新界面
               
// 需要一个数组 数组中是删除的索引   这个数组可以是多行的索引
           
// 下面的刷新方法是对行进行操作的
            [
self . tableView deleteRowsAtIndexPaths : @[ indexPath ] withRowAnimation :( UITableViewRowAnimationLeft )];
        }
else {
           
// 添加
           
        
//  [self.firstDataArray insertObject:str atIndex:[self.firstDataArray count] - 1];
            [
self . firstDataArray insertObject :str atIndex :indexPath. row ];
            [
self . tableView insertRowsAtIndexPaths : @[ indexPath ] withRowAnimation :( UITableViewRowAnimationLeft )];
           
i ++;
        }
    }
else {
       
// 第二分区
       
// 再判断编辑样式
       
if (editingStyle == UITableViewCellEditingStyleDelete ) {
           
// 删除
            [
self . secondDataArray removeObjectAtIndex :indexPath. row ];
           
// 刷新界面
           
// 下面方法 用在删除数据的时候刷新界面
           
// 需要一个数组 数组中是删除的索引   这个数组可以是多行的索引
            [
self . tableView deleteRowsAtIndexPaths : @[ indexPath ] withRowAnimation :( UITableViewRowAnimationLeft )];


        }
else {
           
// 添加
            [
self . secondDataArray insertObject :str1 atIndex :indexPath. row ];
            [
self . tableView insertRowsAtIndexPaths : @[ indexPath ] withRowAnimation :( UITableViewRowAnimationBottom )];
           
j ++;
        }
    }
}
二、tableView的移动
tableView 移动的步骤 :
 
 *  1.
tableView成为可编辑状态
  /** 编辑按钮触发方法
 
 *  1.
激活编辑状态
 *
 *
 */

- (
void )rightButton:( UIBarButtonItem *)rightButton
{
  
// 开启 UITableView 编辑状态
   
// self.tableView.editing 默认是 NO
    [
self . tableView setEditing :! self . tableView . editing animated : YES ];
   
  
// 更改按钮的标题
   
if ( _tableView . editing ) {
        rightButton.
title = @" 完成 " ;
    }
else {
        rightButton.
title = @" 编辑 " ;
    }
}
 *  2. 返回(指定)可以被移动的分区的行  (默认的是YES)
  - ( BOOL )tableView:( UITableView *)tableView canMoveRowAtIndexPath:( NSIndexPath *)indexPath
{
   
return YES ;
}
 *  3. 完成移动后  -1- 更新数据  -2- 刷新界面
// 移动完成

- (
void )tableView:( UITableView *)tableView moveRowAtIndexPath:( NSIndexPath *)sourceIndexPath toIndexPath:( NSIndexPath *)destinationIndexPath
// sourceIndexPath 来源的索引   ( 拿起来 cell 的位置 )
// destinationIndexPath  目的地 / 终点的索引   ( 将来要放下 cell  的位置 )
{
   
// 分两种情况 1. section 的移动 ( 同区移动 ), 不同 section 之间的 移动 ( 跨区移动 )
   
if (sourceIndexPath. section == destinationIndexPath. section ) {
       
// 同区
       
if (sourceIndexPath. section == 0 ) {
           
// 操作第一分区数组
           
// 先保存一下来源索引处的数据
           
NSString *str = self . firstDataArray [sourceIndexPath. row ];
           
// 再从数组中 按来源索引删除该数据
            [
self . firstDataArray removeObjectAtIndex :sourceIndexPath. row ];
           
// 最后再把保存的数据 插入到目的地的索引处
            [
self . firstDataArray insertObject :str atIndex :destinationIndexPath. row ];
           
           
// 刷新页面
            [
self . tableView moveRowAtIndexPath :sourceIndexPath toIndexPath :destinationIndexPath];
           
        }
else {
           
// 操作第二分区数组
           
NSString *str = self . secondDataArray [sourceIndexPath. row ];
            [
self . secondDataArray removeObjectAtIndex :sourceIndexPath. row ];
            [
self . secondDataArray insertObject :str atIndex :destinationIndexPath. row ];
            [
self . tableView moveRowAtIndexPath :sourceIndexPath toIndexPath :destinationIndexPath];
        }
    }
else {
       
// 跨区
     }

4.重要的.
***** iOS 中是不允许跨区移动的
// 系统有一个自带的方法限制跨区移动


// 只要拖动 就会触发这个方法
-(
NSIndexPath *)tableView:( UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:( NSIndexPath *)sourceIndexPath toProposedIndexPath:( NSIndexPath *)proposedDestinationIndexPath
{
   
// sourceIndexPath 来源的索引   ( 拿起来 cell 的位置 )
   
// proposedDestinationIndexPath  推荐 / 建议的 目的地 / 终点的索引   ( 将来要放下 cell  的位置 )
   
if (sourceIndexPath. section == proposedDestinationIndexPath. section ) {
       
return proposedDestinationIndexPath;
    }
else {
       
return sourceIndexPath;
    }
}

三、UITableViewController

 *  UITableViewController
        1.
自带了一个 self.tableView 根屏幕一样大
 
        2.
代理协议 数据源 已经给写好了
       
        3. 实现了一部分 代理方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值