国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

IOSUITableView拖動排序功能

2019-11-14 18:30:05
字體:
供稿:網(wǎng)友

  UITbableView作為列表展示信息,除了展示的功能,有時還會用到刪除,排序等功能,下面就來講解一下如何實現(xiàn)排序。

  排序是當(dāng)表格進入編輯狀態(tài)后,在單元格的右側(cè)會出現(xiàn)一個按鈕,點擊按鈕,就可以拖動單元格,移動位置,進行手動排序。

 

使用系統(tǒng)自帶拖動排序功能的步驟:

1、讓tableView進入編輯狀態(tài),也就是設(shè)置它的editing為YES

2、返回編輯模式,也就是實現(xiàn)UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回UITableViewCellEditingStyleNone模式。如果不實現(xiàn),默認返回的就是刪除模式

3、實現(xiàn)tableView:moveRowAtIndexPath:toIndexPath方法,只要實現(xiàn)該方法,就能實現(xiàn)單元格的拖動排序,但只是實現(xiàn)了表面的排序,并沒有修改真實地數(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 = @"購物車";    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ù)存到模型對象中,然后把對象存到數(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 點擊編輯按鈕- (IBAction)clickEditBtn:(UIButton *)sender {        //設(shè)置tableview編輯狀態(tài)    BOOL flag = !_tableView.editing;    [_tableView setEditing:flag animated:YES];    _editBtn.selected = flag;}#pragma mark 選擇編輯模式,添加模式很少用,默認是刪除-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleNone;}#pragma mark 排序 當(dāng)移動了某一行時候會調(diào)用//編輯狀態(tài)下,只要實現(xiàn)這個方法,就能實現(xiàn)拖動排序-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    // 取出要拖動的模型數(shù)據(jù)    Goods *goods = _goodsAry[sourceIndexPath.row];    //刪除之前行的數(shù)據(jù)    [_goodsAry removeObject:goods];    // 插入數(shù)據(jù)到新的位置    [_goodsAry insertObject:goods atIndex:destinationIndexPath.row];}@end

 

  想要了解更多內(nèi)容的小伙伴,可以點擊查看源碼,親自運行測試。

  疑問咨詢或技術(shù)交流,請加入官方QQ群:JRedu技術(shù)交流 (452379712)

 

作者:杰瑞教育
出處:http://www.survivalescaperooms.com/jerehedu/ 
本文版權(quán)歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 呼图壁县| 南城县| 怀宁县| 巴林左旗| 峨眉山市| 察雅县| 巴中市| 利辛县| 江源县| 兴宁市| 交城县| 延津县| 湘潭县| 玛纳斯县| 宁德市| 来宾市| 囊谦县| 遵化市| 长泰县| 霍邱县| 晋江市| 铜鼓县| 新疆| 涞源县| 高清| 思茅市| 普陀区| 大邑县| 洛宁县| 隆子县| 阿合奇县| 蓬溪县| 南岸区| 同仁县| 丰原市| 阿坝| 延津县| 盐边县| 巴林左旗| 阳信县| 渭南市|