UI基础-UITableView 编辑

tableView编辑包括:cell的添加,cell的删除编辑的步骤:1.让tableView处于编辑状态- (void)setEditing:(BOOL)editing animated:(BOOL)animated;2.指定tableView哪些行可以编辑- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:(NSInd
摘要由CSDN通过智能技术生成

tableView编辑

包括:cell的添加,cell的删除

编辑的步骤:

1.让tableView处于编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

2.指定tableView哪些行可以编辑

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

3.指定tableView编辑的样式(添加、删除)

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

4.完成编辑,提交(先操作数据源,再修改UI)

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

tableView移动

移动的步骤:

1、让tableView处于编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

2.指定tableView哪些⾏行可以移动

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

3.移动完成

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
*)destinationIndexPath

移动过程

监测移动过程,实现限制跨区移动

- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toProposedIndexPath:(NSIndexPath
*)proposedDestinationIndexPath

UITableViewController

UITableViewController继承自UIViewController,自带一个tableView
self.view不是UIView而是UITableView
datasource和delegate默认都是self(UITableViewController)
开发中只需要建立UITableViewController子类

总结

编辑和移动的共同点都是首先要进入编辑状态,结束后要先修改数组或字典中的数据,再更改UI。

例题:
1.完成界面如下:
这里写图片描述
新建工程,创建一个视图控制器作为导航控制器的根视图控制器

RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navC;
[navC release];
[rootVC release];

2.在根视图控制器中加载数据

-(void)setUpData
{
    NSArray *arr = @[@"0", @"1", @"2", @"3", @"添加"];
    self.firstArr = [NSMutableArray arrayWithArray:arr];
    self.secondArr = [NSMutableArray arrayWithArray:arr];;
}

3.在根视图控制器添加TableView子视图

- (void)addTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.view addSubview:self.tableView];
    [self.tableView release];
}

4.实现两个必须实现的代理方法(记得添加代理)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值