34.tableView的编辑

1.基本概念

  1. tableView的编辑:cell的添加、删除。使用场景:删除一个下载好的视频,删除联系人;插⼊一条新的聊天记录等
  2. 编辑的步骤
    1) 让tableView处于编辑状态-TableView方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
    2) 指定tableView哪些行可以编辑-TableView DataSource方法- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath;
    3) 指定tableView编辑的样式(添加、删除)-TableView Delegate方法- (UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath;
    4) 编辑完成(先操作数据源,再修改UI)-TableView DataSource方法- (void)tableView:(UITableView *)tableView commitEditingStyle:
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
  3. 移动的步骤
    1) 让tableView处于编辑状态-TableView方法- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
    2) 指定tableView哪些行可以移动-TableView DataSource⽅方法- (BOOL)tableView:(UITableView )tableView canMoveRowAtIndexPath:(NSIndexPath )indexPath;
    3) 移动完成-TableView DataSource⽅方法- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
    (NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath )destinationIndexPath
    4) 监测移动过程,实现限制跨区移动-TableView方法- (NSIndexPath )tableView:(UITableView )tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath )sourceIndexPath toProposedIndexPath:(NSIndexPath )proposedDestinationIndexPath
  4. UITableViewController继承自UIViewController,自带一个tableView self.view不是UIView⽽而是UITableView datasource和delegate默认都是self(UITableViewController) 开发中只需要建立UITableViewController子类,UITableViewController是封装好了各种delegate和datasource,能提高我们开发速度。
  5. 无论编辑还是移动,都先让tableView进入编辑状态,然后指定哪些行可以编辑或移动,编辑或者移动结束,要先修改数组或字典中的数据,再更改UI。
  6. 哪些行可以进行编辑(默认是YES)–>setEditing:animated:(默认是NO)–>哪些行可以进行移动(默认是YES)

2.简单运用

#import "MainViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *arr;
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值