iphone: 可编辑的tableView Move&Delete

简介:

设置一个Button,点击可以删除,移动排序tableView,效果图如下:左边为移动,右边为删除

                                     

先说移动:

button要设置IBAction

复制代码
- (IBAction)toggleMove {
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    
    if (self.tableView.editing)
        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    else
        [self.navigationItem.rightBarButtonItem setTitle:@"Move"];
}
复制代码

self.tableView.editing属性是个bool型,表示tableView的编辑状态

这里设置button为两种文字,对应不同状态。

然后实现三个委托方法即可:

复制代码
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath {
    NSUInteger fromRow = [fromIndexPath row];
    NSUInteger toRow = [toIndexPath row];
    
    id object = [list objectAtIndex:fromRow];
    [list removeObjectAtIndex:fromRow];
    [list insertObject:object atIndex:toRow];
}
复制代码

删除的则更简单了。

其button的action与上面一样,

只要实现一个委托方法:

复制代码
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSUInteger row = [indexPath row];
    [self.list removeObjectAtIndex:row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
}
复制代码

 

也可以将二者结合起来,实现上面的4哥委托方法就行。只是要注意在editingStyleForRowAtIndexPath 方法种,返回UITableViewCellEditingStyleDelete

效果图如下:

   
本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/06/09/2543091.html ,如需转载请自行联系原作者
相关文章
|
8月前
|
开发工具 iOS开发
iOS 逆向编程(十一)iPhone 终端支持中文输入与vim命令(编辑文件)
iOS 逆向编程(十一)iPhone 终端支持中文输入与vim命令(编辑文件)
64 0
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
150 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
225 0
|
Web App开发 缓存 开发工具
|
存储 iOS开发 计算机视觉