Cell 的增删和移动


// 设置 Cell的 编辑模式 : 删除 。 增加 ,None

#pragma mark -TableView delegate method

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

   if (indexPath.row==1) {

        returnUITableViewCellEditingStyleInsert;

    }else{

        returnUITableViewCellEditingStyleDelete;

    }

}


/**

 *  2,设置当前视图控制器的"视图"编辑状态

 *  @param editing  是否设置当前控制器视图为可编辑

 *  @param animated 是否有动画效果

 *

 */

//-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

//    if (self.tableView.editing) {

        //  为 self.tableView视图 调用 setEditing: 方法后,所有的cell都将触发编辑状态

//        [self.tableView setEditing:NO animated:YES];

//    }else{

//        [self.tableView setEditing:YES animated:YES];

//    }

//}



/**

 *   设置 该 indexPath.row 是否可被编辑 ;

 */

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    // Return NO if you do not want the specified item to be editable.

   if (indexPath.row==0) {

       return NO;

    }

    return YES;

}




// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    // delete row

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source  :这行注释非常重要,说明了要在这应该做的操作:1,删除数据 2,移除行

        [_fontsArrremoveObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];

    }

    // insert row

    elseif (editingStyle ==UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

        //同样上面的官方注释已经很清楚的说明了,要添加一行cell 要作的步骤:1,添加数据 2,插入行

       NSString *newLine=@"New Line";

        [_fontsArrinsertObject:newLine atIndex:indexPath.row+1];

       NSIndexPath *newIndexPath=[NSIndexPathindexPathForRow:indexPath.row+1inSection:indexPath.section];

        [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationTop];

    }   

}



/**

 *   对Cell 的移动设置

 */


// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

  NSString *testFromIndex=[[_fontsArrobjectAtIndex:fromIndexPath.row]retain];

    [_fontsArrremoveObject:testFromIndex];

    [_fontsArrinsertObject:testFromIndex atIndex:toIndexPath.row];

}



/**

 *  tableView cell进行有限制的移动设定;

 *

 */

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    // Return NO if you do not want the item to be re-orderable.

    return YES;

}


对于tableview 的编辑设置方法的分析:

1, setEditing: / setEditing:ainmation:
2,  tableView:canMoveRowAtIndexPath:
上面方法1, 调用后回激发 所有cell 的编辑状态
下面方法2, 这个是当我们调用方法一或者左滑动时针对单行或者所有调用的方法 ,当设置为return yes; 时所有的cell都将具备能触发编辑状态,
 方法1,可以不用设置!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值