4.11 Moving Cells and Sections in Table Views

本文详细介绍了如何在iOS应用中使用UITableView的moveRowAtIndexPath:toIndexPath:和moveSection:toSection:方法来移动行和Section,包括注意事项及示例代码。

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

移动tableview中的行和Section
简单的说移动行用 moveRowAtIndexPath:toIndexPath:
移动Section用 moveSection:toSection:

有一点需要注意的是,这里是移动,不是交换位置。
比如说某个section中有3行,按顺序是 row1,row2,row3,他们的位置分别是indexPath1,indexPath2,indexPath3。
现在moveRowAtIndexPath:indexPath3 toIndexPath:indexPath1  你猜是啥结果,是row3,row2,row1吗?那就错了。因为是把row3移到那个位置,而不是与那个位置的row交换。所以正确结果是row3,row1,row2。section也一样。
下面是本人小例子,仅供参考
NSIndexPath * markIndexPath=nil;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
    if (markIndexPath == nil) {
        markIndexPath = indexPath;
    }else if ((indexPath.section == markIndexPath.section) && (indexPath.row != markIndexPath.row)){
        //记得先改变数据源数据
        [tableView moveRowAtIndexPath:indexPath toIndexPath:markIndexPath];
        markIndexPath = nil;
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [tableView deselectRowAtIndexPath:markIndexPath animated:YES];//没实现,谁告诉为什么
    }else if(indexPath.section != markIndexPath.section){
  //记得先改变数据源数据
        [tableView moveSection:markIndexPath.section toSection:indexPath.section];
        markIndexPath = nil;
    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值