iOS UITableView 拖動(dòng)排序?qū)崿F(xiàn)代碼
UITbableView作為列表展示信息,除了展示的功能,有時(shí)還會(huì)用到刪除,排序等功能,下面就來講解一下如何實(shí)現(xiàn)排序。
排序是當(dāng)表格進(jìn)入編輯狀態(tài)后,在單元格的右側(cè)會(huì)出現(xiàn)一個(gè)按鈕,點(diǎn)擊按鈕,就可以拖動(dòng)單元格,移動(dòng)位置,進(jìn)行手動(dòng)排序。
使用系統(tǒng)自帶拖動(dòng)排序功能的步驟:
1、讓tableView進(jìn)入編輯狀態(tài),也就是設(shè)置它的editing為YES
2、返回編輯模式,也就是實(shí)現(xiàn)UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回UITableViewCellEditingStyleNone模式。如果不實(shí)現(xiàn),默認(rèn)返回的就是刪除模式
3、實(shí)現(xiàn)tableView:moveRowAtIndexPath:toIndexPath方法,只要實(shí)現(xiàn)該方法,就能實(shí)現(xiàn)單元格的拖動(dòng)排序,但只是實(shí)現(xiàn)了表面的排序,并沒有修改真實(shí)地?cái)?shù)據(jù)
4、在方法中完成數(shù)據(jù)模型的更新
代碼:
// ViewController.m // JRTableView刪除 // // Created by jerehedu on 15/6/11. // Copyright (c) 2015年 jerehedu. All rights reserved. // #import "ViewController.h" #import "Goods.h" @interface ViewController ()<UITableViewDataSource, UITableViewDelegate> { UITableView *_tableView; //列表 NSMutableArray *_goodsAry; //商品數(shù)組 UIButton *_editBtn; //編輯按鈕 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //添加標(biāo)題 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)]; titleLabel.text = @"購(gòu)物車"; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.backgroundColor = [UIColor redColor]; titleLabel.textColor = [UIColor whiteColor]; [self.view addSubview:titleLabel]; //添加編輯按鈕 _editBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _editBtn.frame = CGRectMake(self.view.frame.size.width-60, 25, 50, 34); [_editBtn setTitle:@"編輯" forState:UIControlStateNormal]; [_editBtn setTitle:@"完成" forState:UIControlStateSelected]; _editBtn.titleLabel.font = [UIFont systemFontOfSize:15]; _editBtn.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5]; [self.view addSubview:_editBtn]; [_editBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside]; //添加tableview _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; //取數(shù)據(jù) NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]]; //把數(shù)據(jù)存到模型對(duì)象中,然后把對(duì)象存到數(shù)組中 _goodsAry = [NSMutableArray array]; for (int i=0; i<ary.count; i++) { Goods *good = [Goods goodsWithDic:ary[i]]; [_goodsAry addObject:good]; } } #pragma mark 數(shù)據(jù)源 返回有幾行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _goodsAry.count; } #pragma mark 每行顯示內(nèi)容 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *idGood = @"goods"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idGood]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idGood]; } Goods *good = _goodsAry[indexPath.row]; cell.imageView.image = [UIImage imageNamed:good.icon]; cell.textLabel.text = good.name; cell.detailTextLabel.text = good.details; cell.detailTextLabel.numberOfLines = 6; cell.detailTextLabel.textColor = [UIColor brownColor]; return cell; } #pragma mark 選中行 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 取消選中狀態(tài) [tableView deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark 設(shè)置行高 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 110; } #pragma mark 點(diǎn)擊編輯按鈕 - (IBAction)clickEditBtn:(UIButton *)sender { //設(shè)置tableview編輯狀態(tài) BOOL flag = !_tableView.editing; [_tableView setEditing:flag animated:YES]; _editBtn.selected = flag; } #pragma mark 選擇編輯模式,添加模式很少用,默認(rèn)是刪除 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } #pragma mark 排序 當(dāng)移動(dòng)了某一行時(shí)候會(huì)調(diào)用 //編輯狀態(tài)下,只要實(shí)現(xiàn)這個(gè)方法,就能實(shí)現(xiàn)拖動(dòng)排序 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { // 取出要拖動(dòng)的模型數(shù)據(jù) Goods *goods = _goodsAry[sourceIndexPath.row]; //刪除之前行的數(shù)據(jù) [_goodsAry removeObject:goods]; // 插入數(shù)據(jù)到新的位置 [_goodsAry insertObject:goods atIndex:destinationIndexPath.row]; } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS13即將到來,iOS推送DeviceToken適配方案詳解
這篇文章主要介紹了iOS13即將到來,iOS推送DeviceToken適配方案詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09學(xué)習(xí)iOS自定義導(dǎo)航控制器UINavigationController
這篇文章主要為大家詳細(xì)介紹了iOS自定義導(dǎo)航控制器UINavigationController,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新
這篇文章主要介紹了iOS App中UITableView的創(chuàng)建與內(nèi)容刷新,講解了UITableView一些基本的樣式與cell的設(shè)置及刷新,需要的朋友可以參考下2016-04-04iOS實(shí)現(xiàn)app間跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)app間跳轉(zhuǎn)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05iOS項(xiàng)目開發(fā)鍵盤彈出遮擋輸入框問題解決方案
大家在用IOS開發(fā)項(xiàng)目的時(shí)候,經(jīng)常出現(xiàn)鍵盤彈出遮擋輸入框問題,小編給大家整理的這個(gè)問題的處理方法,一起學(xué)習(xí)下。2018-01-01使用UItableview在iOS應(yīng)用開發(fā)中實(shí)現(xiàn)好友列表功能
這篇文章主要介紹了使用UItableview在iOS應(yīng)用開發(fā)中實(shí)現(xiàn)一個(gè)好友列表功能的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS中讀取照片庫(kù)及保存圖片或視頻到照片庫(kù)的要點(diǎn)解析
iOS中保存到本地的圖片視頻都會(huì)被匯總到系統(tǒng)的PhotoLibrary中,這里我們就來看一下iOS中讀取照片庫(kù)及保存圖片或視頻到照片庫(kù)的要點(diǎn)解析2016-06-06