UITableView 拖拽移动单元格

小萌做开发的时候,项目需求是:拖拽单元格随意变换位置,变换位置之后仍然可以编辑其它,界面需求如下图所示。

UITableView 拖拽移动单元格_第1张图片
image.png

小萌在网上找的例子自己使用的时候发现很多问题,下面发一种小萌亲测可行的方案,以及补充小萌一直以来忽略的知识点。NSMutableArray的中removeObjectAtIndex和的removeObject的正确使用方法

下面主要代码:

@interface AddController ()

@property(nonatomic,strong)UITableView *myTableView;

@property(nonatomic,strong)NSMutableArray *dataArr;

/*****发文本******/
@property(nonatomic,strong)UIButton *textButton;


/******发图片********/

@property(nonatomic,strong)UIButton *imageButton;

/******上传图片的时候用到********/
@property(nonatomic,strong)NSIndexPath *selePatch;
@end



- (void)viewDidLoad {
    
    [super viewDidLoad];
   

    self.dataArr = [NSMutableArray array];
    
    [self.view addSubview:self.myTableView];
 //这两行很主要,开启可移动
    self.myTableView.allowsMultipleSelection=YES;
    self.myTableView.editing=YES;
    
    
    
//发图片按钮
    self.imageButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 84, SCREEN_HEIGHT - iPhoneXTabBarHeight - 85, 64, 65)];
    [self.imageButton setImage:[UIImage imageNamed:@"icon_editdetail_addimg"] forState:UIControlStateNormal];
    [self.imageButton addTarget:self action:@selector(addimageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.imageButton];
    
    
    //发文本按钮
    self.textButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 84*2, SCREEN_HEIGHT - iPhoneXTabBarHeight - 85, 64, 65)];
    [self.textButton setImage:[UIImage imageNamed:@"icon_editdetail_addtype"] forState:UIControlStateNormal];
    [self.textButton addTarget:self action:@selector(addtextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.textButton];
    
    //导航条右边的保存按钮,我是自定义的,不是自定义的朋友请修改
    [self.navBar.titleRight setTitle:@"保存" forState:UIControlStateNormal];
    [self.navBar.titleRight setTitleColor:UIColorFromRGB(0xFF4055) forState:UIControlStateNormal];
    [self.navBar.titleRight addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.view.backgroundColor = UIColorFromRGB(0xf7f7f7);
    
    
    
    
}

#pragma mark -addimageAction 添加图片  发图片
- (void)addimageAction:(UIButton *)btn{
    
    NSMutableDictionary *editDic = [NSMutableDictionary dictionary];
    
    [editDic setValue:@"image" forKey:@"type"];
    [editDic setValue:@"" forKey:@"content"];
    [editDic setValue:@"" forKey:@"convertUrl"];
    
    
    [self.dataArr addObject:editDic];

//刷新
    [self.myTableView reloadData];
    
}


#pragma mark -addtextAction 添加文本   发文本
- (void)addtextAction:(UIButton *)btn{
    
    NSMutableDictionary *editDic = [NSMutableDictionary dictionary];
    
    [editDic setValue:@"text" forKey:@"type"];
    [editDic setValue:@"" forKey:@"content"];
    [self.dataArr addObject:editDic];
    
    [self.myTableView reloadData];
    
}


//删除
#pragma mark - deleAction  删除
- (void)deleAction:(UIButton *)btn{
    
    
//    //取出移动row数据
//    NSDictionary *rowDic = self.dataArr[btn.tag - 3000];
//    //从数据源中移除该数据
//    [self.dataArr removeObject:rowDic];
//

//用下面这种,上面是小萌吃亏的地方removeObject和removeObjectAtIndex是有区别的,看上面的链接
    [self.dataArr removeObjectAtIndex:btn.tag - 3000];
    NSLog(@"%@",self.dataArr);
    
    [self.myTableView reloadData];
}



#pragma mark - saveAction 保存
- (void)saveAction{
    
    
    if (self.dataArr.count == 0) {
        
        CustomToastView *toast = [[CustomToastView alloc]initWithView:self.view text:@"保存的数据不能为空" duration:KDuration];
        [toast show];
        
        return ;
    }
    
    
    
    
    NSLog(@"%@",self.dataArr);
    
    for (int i = 0; i *)info {

    //UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];


  //上传原图
    UIImage *imageOrigina=[info objectForKey:UIImagePickerControllerOriginalImage];

   

    [picker dismissViewControllerAnimated:YES completion:^{
    }];

}

你可能感兴趣的:(UITableView 拖拽移动单元格)