UITableView编辑模式

本文详细介绍如何在iOS应用中使用UITableView的编辑模式,包括插入、删除行及行的移动操作。通过对代理方法的实现,展示了如何定制编辑样式以及响应编辑操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依据http://www.winddisk.com/2012/07/05/uitableview_edit_mod/

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

   UITableView *_tableView;

   NSMutableArray *_dataArray;

}


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    self.navigationItem.rightBarButtonItem =self.editButtonItem;

    CGRect frame = [UIScreenmainScreen].bounds;

    frame.size.height = frame.size.height - 44;

    _tableView = [[UITableViewalloc] initWithFrame:framestyle:UITableViewStylePlain];

    _tableView.delegate =self;

    _tableView.dataSource =self;


    [self.viewaddSubview:_tableView];

    _dataArray = [[NSMutableArrayalloc] initWithCapacity:20];

   for (int i =0; i < 20; i++) {

        [_dataArrayaddObject:[NSStringstringWithFormat:@"%d",i]];

    }

}


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

{

    [supersetEditing:editing animated:animated];

    //1、设置UIViewController的编辑模式时,同时设置tableView的编辑状态,也可以单独设置

    [_tableViewsetEditing:editing animated:animated];

}


/*tableView在向自身发送setEditing:animated:消息的前后,会向其delegate分别发送tableView:willBeginEditingRowAtIndexPath: ,tableView:didEndEditingRowAtIndexPath:消息。在这些方法中可相应更新tableView的显示。How does the Twitter iPhone app implement side swiping on a table?中通过实现tableView:willBeginEditingRowAtIndexPath:方法使得用户在tableView的行上swipe时可滑出菜单。

 */

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

    //setEditing:animated:

}


- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

    //setEditing:animated:

}

    

#pragma mark -tableView delegate and dataSource

#pragma mark @required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [_dataArraycount];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString *cellID =@"cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];

        

    }

    cell.textLabel.text = [_dataArrayobjectAtIndex:indexPath.row];

   return cell;

}

#pragma mark -tableView delegate and dataSource

#pragma mark  @edit  method

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

{

    //2tableView收到setEditing:animated:时,调用此函数,询问哪些cell可编辑,默认实现此方法

   if (indexPath.row <=11) {

       return YES;

    }

    return NO;

}


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

{

    //2tableView收到setEditing:animated:时,调用此函数,询问是否显示移动标志,不实现时默认显示

    return YES;

}

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

{

    //3、返回具体的编辑模式,若不实现,默认删除模式

   if (indexPath.row %2 == 0) {

        returnUITableViewCellEditingStyleDelete;

    }else{

        returnUITableViewCellEditingStyleInsert;

    }

       

}


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

{

    //4、根据具体的编辑模式进行插入删除

    if (editingStyle ==UITableViewCellEditingStyleInsert) {

        [_dataArrayinsertObject:@"new"atIndex:indexPath.row];

        [_tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationAutomatic];

        

    }elseif(editingStyle ==UITableViewCellEditingStyleDelete){

        [_dataArrayremoveObjectAtIndex:indexPath.row];

        [_tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

      

        

    }

}


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

{

    //3.用户拖动某行sourceIndexPath经过目标行proposedDestinationIndexPath上方时,调用此函数询问是否可以移动,若不能移动则返回一个新的目的indexPath,否则直接返回proposedDestinationIndexPath,若无特别要求不需要实现

   if (proposedDestinationIndexPath.row ==5) {

        return [NSIndexPathindexPathForRow:8inSection:0];

    }

   return proposedDestinationIndexPath;

}

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

{

    //4.实现此方法时,进入编辑模式后右侧会出现移动标志

    //根据实际移动情况更改tableView

   if (sourceIndexPath == destinationIndexPath) {

       return;

    }

   id object = [_dataArrayobjectAtIndex:sourceIndexPath.row];

    [_dataArrayremoveObjectAtIndex:sourceIndexPath.row];

   

    [_dataArrayinsertObject:object atIndex:destinationIndexPath.row];

    

}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //改变delete按钮上要显示的文字

    return@"我要删除你";

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值