【UITableView】UITableView 编辑

//某一行是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPatt
{
    return NO;
}
//确认移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if(sourceIndexPath.section == destinationIndexPath.section){
        //获取要移动元素所在的组(子数组)
        NSMutableArray *subArray = [_dataArray objectAtIndex:sourceIndexPath.section];
        //获得要移动的对象
        DataItem *item = [subArray objectAtIndex:sourceIndexPath.row];
        //删除
        [subArray removeObject:item];
        //将他插入到新位置
        [subArray insertObject:item atIndex:destinationIndexPath.row];
      
    }else{
        //获得要移动的对象
        NSMutableArray *subArray = [_dataArray objectAtIndex:sourceIndexPath.section];
        DataItem *item = [subArray objectAtIndex:sourceIndexPath.row];
        [subArray removeObject:item];
        //获得目标数组
        NSMutableArray *destArray = [_dataArray objectAtIndex:destinationIndexPath.section];
        //插入新元素
        [destArray insertObject:item atIndex:destinationIndexPath.row];
        
    }
}
 
//编辑方式 删除或者插入
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if(indexPath.row%2!=0){
        return  UITableViewCellEditingStyleInsert;
   }
    
    return UITableViewCellEditingStyleDelete;
}
 
//是否可以编辑(默认是可以编辑的)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  YES;//indexPath.row%2==0;
}
//确认编辑的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleInsert){
        NSMutableArray *subArray = [_dataArray objectAtIndex:indexPath.section];
        DataItem *oldItem = [subArray lastObject];
        NSRange range =[oldItem.title rangeOfString:@":"];
        NSString *replaceStr = [oldItem.title substringFromIndex:range.location];
        DataItem *item = [[DataItem alloc]init];
        item.title = [oldItem.title stringByReplacingOccurrencesOfString:replaceStr withString:[NSString stringWithFormat:@"%d行",[subArray count]]];
        item.state = 0;
        [subArray insertObject:item atIndex:indexPath.row];
        [_myTalbelView reloadData];
    }else {
        NSMutableArray *subArray = [_dataArray objectAtIndex:indexPath.section];
        [subArray removeObjectAtIndex:indexPath.row];
        //刷新界面(必须加这句,不然点删除删不了)
        [_myTalbelView reloadData];
    }
}
//索引对应关系
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return (index+2)%26;
}
//删除样式
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row%4==0){
        return @"删除";
    }
    return @"Remove";
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值