IOS UITableView拖动排序功能

摘要:
UITableView用作显示信息的列表。除了显示功能外,有时还会使用删除、排序等功能。下面是如何实现排序。排序意味着当表格处于编辑状态时,单元格右侧会出现一个按钮。单击按钮拖动单元格并移动位置以手动排序。

  UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,排序等功能,下面就来讲解一下如何实现排序。

  排序是当表格进入编辑状态后,在单元格的右侧会出现一个按钮,点击按钮,就可以拖动单元格,移动位置,进行手动排序。

IOS UITableView拖动排序功能第1张IOS UITableView拖动排序功能第2张

使用系统自带拖动排序功能的步骤:

1、让tableView进入编辑状态,也就是设置它的editing为YES

2、返回编辑模式,也就是实现UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回UITableViewCellEditingStyleNone模式。如果不实现,默认返回的就是删除模式

3、实现tableView:moveRowAtIndexPath:toIndexPath方法,只要实现该方法,就能实现单元格的拖动排序,但只是实现了表面的排序,并没有修改真实地数据

4、在方法中完成数据模型的更新

代码:

//  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; //商品数组
    
    UIButton *_editBtn; //编辑按钮
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加标题
    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];
    
    //取数据
    NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]];
    
    //把数据存到模型对象中,然后把对象存到数组中
    _goodsAry = [NSMutableArray array];
    for (int i=0; i<ary.count; i++) {
        Goods *good = [Goods goodsWithDic:ary[i]];
        [_goodsAry addObject:good];
    }
}

#pragma mark 数据源  返回有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _goodsAry.count;
}

#pragma mark 每行显示内容
-(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
{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark 设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

#pragma mark 点击编辑按钮
- (IBAction)clickEditBtn:(UIButton *)sender {
    
    //设置tableview编辑状态
    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 排序 当移动了某一行时候会调用
//编辑状态下,只要实现这个方法,就能实现拖动排序
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // 取出要拖动的模型数据
    Goods *goods = _goodsAry[sourceIndexPath.row];
    //删除之前行的数据
    [_goodsAry removeObject:goods];
    // 插入数据到新的位置
    [_goodsAry insertObject:goods atIndex:destinationIndexPath.row];
}

@end

  想要了解更多内容的小伙伴,可以点击查看源码,亲自运行测试。

  疑问咨询或技术交流,请加入官方QQ群:JRedu技术交流 (452379712)

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

免责声明:文章转载自《IOS UITableView拖动排序功能》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇gh-ost安装Visual Studio 14 初试,vNext下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CoreData使用方法二:NSFetchedResultsController实例操作与解说

        学习了NSFetchedResultsController。才深深的体会到coredata的牛逼之处。原来Apple公司弄个新技术。不是平白无故的去弄,会给代码执行到来非常大的优点。coredata不仅能让我们大大的降低代码量。还最大化的提高执行效率。        就拿NSFetchedResultsController来说吧,他是和...

IOS学习:UITableView使用详解3 分组表的简单使用

IOS学习:UITableView使用详解3 分组表的简单使用 使用分组表和使用普通表的方法差不多,他们的不同点有以下几点: 1.分组表的属性必须设置为Grouped的,而不是plain 2.分组表的数据源方法当中numberOfSectionsInTableView:返回分组的个数。 3.可以设置tableView:titleForHeaderInSec...

一个巧妙的实现悬浮的tableViewHeader的方法

之前因为工作需要要实现一个类似的 悬浮+视差的headerView的效果, 研究了好久没研究出来怎么做,最后用UICollectionView +CSStickyHeaderFlowLayout的方法实现了(不得不说CSStickyHeaderFlowLayout真的是一个很强大的库,作者对UICollectionView的理解真的是深刻,建议有空一...

ios tableview 滑动到底部

tableview滑动到底部,根据页面不同 可以有两种方法 第一种: 一般样式的tableview 没有头和尾的 #pragma mark - 滑到最底部 - (void)scrollTableToFoot:(BOOL)animated { NSInteger s = [self.tableView numberOfSections]; //有多少组 i...

iOS UIKit:TableView之编辑模式(3)

一般table view有编辑模式和正常模式,当table view进入编辑模式时,会在row的左边显示编辑和重排控件,如图 42所示的编辑模式时的控件布局;左边的editing control有表 61的两种图标。 表 61 table view编辑控件 图标 描述 Deletion控件 Insertion控件 若ta...

TableView之表头、表尾,区头、区尾!

一、UITableView的UITableViewStyle   样式分为UITableViewStylePlain和UITableViewStyleGrouped两种;   plain样式下区头和区尾是悬浮的(即拖动表的时候区头和区尾不会消失,一直显示在界面上);   grouped样式区头和区尾是随表一起滑动的。静态的tableview需要分区时(XI...