UITableView 拖拽移动单元格

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

image.png

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

下面主要代码:

@interface AddController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate>

@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<self.dataArr.count; i ++ ) {
        
        NSMutableDictionary *cellDic = [NSMutableDictionary dictionary];
        
        [cellDic addEntriesFromDictionary:self.dataArr[i]]  ;
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:i];
        DescAndTextCell *vd =(DescAndTextCell *)[self.myTableView  cellForRowAtIndexPath:indexPath];
        [cellDic setValue:[NSString stringWithFormat:@"%d",i + 1] forKey:@"no"];
        if ([cellDic[@"type"] isEqualToString:@"text"]) {
            
            [cellDic setValue:vd.textCellView.text forKey:@"content"];
        }
        
        NSString *contents = cellDic[@"content"];
        
        if (DT_IsEmpty(contents) == YES) {
            
            CustomToastView *toast = [[CustomToastView alloc]initWithView:self.view text:@"保存的数据不能为空" duration:KDuration];
            [toast show];
            
            
            return ;
            
        }
   
    }
    
    
    if (self.block) {
        self.block(self.dataArr);
    }
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}


#pragma mark - tipAction 开启了移动单元格点击作用消失,需要其他的方法
- (void)tipAction:(UIButton *)btn{
    
    NSDictionary *cellDic = self.dataArr[btn.indexP.section];
    
    
    if ([cellDic[@"type"] isEqualToString:@"text"]) {
        
        return ;
    }
    
    
    
    self.selePatch = btn.indexP;
    
    // 类方法
    LCActionSheet *sheet = [LCActionSheet sheetWithTitle:nil buttonTitles:@[@"拍照",@"从手机相册选择"] redButtonIndex:-1 clicked:^(NSInteger buttonIndex) {
        
        
        if (buttonIndex == 0) {
            
            [self takePhoto];
            
        }else if(buttonIndex == 1){
            [self localPhoto];
            
            
        }
    }];
    
    
    [sheet show];
    
}

//必须把编辑模式改成None,默认的是delete
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return UITableViewCellEditingStyleNone;
    
}



#pragma mark 排序当移动了某一行时候会调用

//编辑状态下,只要实现这个方法,就能实现拖动排序---右侧会出现三条杠,点击三条杠就能拖动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{
    
    // 取出要拖动的模型数据
    
    NSDictionary *goods = self.dataArr[sourceIndexPath.section];
    
    //删除之前行的数据
      //[self.dataArr removeObject:goods];千万别这样做哦

    [self.dataArr removeObjectAtIndex:sourceIndexPath.section];
    
    
    // 插入数据到新的位置
    
    [self.dataArr insertObject:goods atIndex:destinationIndexPath.section];
    
    
    NSLog(@"%@",self.dataArr);
    
    
    [self.myTableView reloadData];
    
    
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *indentifer = @"DescAndTextCell";
    
    DescAndTextCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        
        cell = [[DescAndTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
    }

    
    NSDictionary *cellDic = self.dataArr[indexPath.section];
    
    
    if ([cellDic[@"type"] isEqualToString:@"text"]) {
        cell.moveButton.hidden = YES;
        cell.textCellView.delegate = self;
        cell.textCellView.tag = indexPath.section + 500;
        cell.textCellView.hidden = NO;
        cell.imageCellView.hidden = YES;
        cell.textCellView.text = cellDic[@"content"];
        
        
    }else{
        
        cell.moveButton.hidden = NO;
        cell.textCellView.hidden = YES;
        cell.imageCellView.hidden = NO;
        
        NSString *https = cellDic[@"convertUrl"];
        NSLog(@"%@",https);
        
        
        if (DT_IsEmpty(https) == NO) {
            
            cell.photoLabel.hidden = YES;
            cell.imageLabel.hidden = YES;
            cell.photoImage.hidden = YES;
            [cell.imageCellView sd_setImageWithURL:[NSURL URLWithString:https] placeholderImage:[UIImage imageNamed:@"icon-tong-default"]];
            
        }else{
            
            cell.photoLabel.hidden = NO;
            cell.imageLabel.hidden = NO;
            cell.photoImage.hidden = NO;
            
        }
    }
    
    
    cell.deleButton.tag = indexPath.section + 3000;
    [cell.deleButton addTarget:self action:@selector(deleAction:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.tipButton.indexP = indexPath;
    [cell.tipButton addTarget:self action:@selector(tipAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.000001;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}






//失效
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
   
  
}



- (void)textViewDidEndEditing:(UITextView *)textView{
    
    //取出移动row数据
    NSDictionary *rowDic = self.dataArr[textView.tag - 500];
    
    [rowDic setValue:textView.text forKey:@"content"];
    
}



#pragma mark - getter

- (UITableView *)myTableView{
    
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(15, NavBarHeight, SCREEN_WIDTH - 30, SCREEN_HEIGHT-NavBarHeight) style:UITableViewStyleGrouped];
        _myTableView.estimatedRowHeight = 0;
        _myTableView.rowHeight = 130;
        _myTableView.backgroundColor = UIColorFromRGB(0xf7f7f7);
        _myTableView.estimatedSectionHeaderHeight = 0;
        _myTableView.estimatedSectionFooterHeight = 0;
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 11.0, *)) {
            _myTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        _myTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);//iPhoneX这里是88
        _myTableView.scrollIndicatorInsets = _myTableView.contentInset;
    }
    return _myTableView;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 拍照
/*3:从相册选择*/
- (void)localPhoto
{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //资源类型为图片库
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = NO;

    [self presentViewController:picker animated:YES completion:^{

    }];
}


/*4:拍照*/
- (void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    //判断是否有相机
    if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate =  self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = NO;
        //资源类型为照相机
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:^{

        }];

    }else {

        // NSLog(@"该设备不支持拍照");

    }
}



//再调用以下委托:
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

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


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

   

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

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容