移动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;
}
}