UITableView的常用编辑操作

今天刚学了UItableView,在此总结了一些易忘的小操作。代理协议中的方法虽然很多,但是其实是有规律的,很多操作都是要先让某项操作可执行,然后再实现该方法。对应代码就是: 一个方法以BOOL为返回值,一个方法以void为返回值。下面的代码以hero为模型,该模型有三个属性,头像icon、名字name、介绍intro。具体代码如下


一、移动tableView中的行


1.要使每一行为可编辑状态 
self.tableView.editing = YES;
2.使每一行行可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
3.控制器监听到移动后,交换模型数组中两个模型的数据.(不需要刷新)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [self.herosArray exchangeObjectAtIndex:destinationIndexPath.row withObjectAtIndex:sourceIndexPath.row];
}



二、tableView的复制、粘贴、剪贴操作
1.显示拷贝、粘贴选项板
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
2.实现某个方法(判断是不是拷贝、粘贴、剪切方法,是的话重新赋值模型数据,刷新)
// 在tableView的这一行执行某个方法
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    // 1.取出当前行的模型
    Hero *hero = self.herosArray[indexPath.row];
    // 1.1如果当前操作为拷贝操作
    if (action == @selector(copy:)) {
        // 1.2剪贴板记录下该模型的所有数据
        [UIPasteboard generalPasteboard].strings = @[hero.name, hero.intro, hero.icon];
        
    }
    // 如果为粘贴操作,给模型赋值新的数据,并刷新该行
    if (action == @selector(paste:)) {
        hero.name = [UIPasteboard generalPasteboard].strings[0];
        hero.intro = [UIPasteboard generalPasteboard].strings[1];
        hero.icon = [UIPasteboard generalPasteboard].strings[2];
        NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    // 如果当前为剪切操作
    if (action == @selector(cut:)){
        [UIPasteboard generalPasteboard].strings = @[hero.name, hero.intro, hero.icon];
        hero.name = nil;
        hero.icon = nil;
        hero.intro = nil;
        NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        [self.tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    
}



三、在tableView中插入一行


1.修改编辑模式为插入
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleInsert;
}
2.检测插入操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        Hero *hero = [[Hero alloc] init];
        hero.name = @"日番谷、冬狮郎";
        hero.intro = @"十番队、队长";
        hero.icon = @"1385536285032.png";
        [self.herosArray insertObject:hero atIndex:indexPath.row];
        
        // 插入一行,有动画,不需要手动刷新
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}


四、在tableView中删除某一行
1.修改cell的编辑模式为删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewCellEditingStyleInsert;
    return UITableViewCellEditingStyleDelete;
}


2.进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
      
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.herosArray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值