iOS UITableView 拖动排序实现代码

 更新时间:2016年09月27日 10:23:06   投稿:lijiao  
这篇文章主要为大家详细介绍了iOS UITableView 拖动排序实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键详解

    如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键详解

    xcode是苹果公司向开发人员提供的集成开发环境,开发者们经常会使用到,下面这篇文章主要给大家介绍了关于如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键的相关资料,需要的朋友可以参考下。
    2018-04-04
  • IOS 中CATextLayer绘制文本字符串

    IOS 中CATextLayer绘制文本字符串

    这篇文章主要介绍了IOS 中CATextLayer绘制文本字符串的相关资料,希望通过本能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • iOS如何获取当前日期前后N天的时间示例代码

    iOS如何获取当前日期前后N天的时间示例代码

    这篇文章主要给大家介绍了关于iOS如何获取当前日期前后N天的时间的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
    2017-11-11
  • iOS当多个网络请求完成后执行下一步的方法详解

    iOS当多个网络请求完成后执行下一步的方法详解

    在多线程中,有时候我们会遇到一个界面同时有多个网络请求(比如a,b,c,d四个网络请求),在这四个个请求结束后,在请求到数据去做其他操作(UI更新等),下面这篇文章主要给大家介绍了关于iOS当多个网络请求完成后执行下一步的相关资料,需要的朋友可以参考下。
    2017-12-12
  • IOS客户端接入微信支付

    IOS客户端接入微信支付

    对于一个ios的app,如果有一些虚拟的商品或者服务需要通过在线支付来收费的话,一般有几种主流的选择。如果是通过APP调用支付平台APP的思路的话,一个是调起支付宝客户端,一个则是调起微信支付。本文给大家分享ios客户端接入微信支付,需要的朋友可以参考下
    2015-09-09
  • swift3.0网络图片缓存原理简析

    swift3.0网络图片缓存原理简析

    这篇文章主要为大家简析了swift3.0网络图片缓存原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • iOS关键字static extern const使用示例详解

    iOS关键字static extern const使用示例详解

    这篇文章主要为大家介绍了iOS关键字static extern const使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • iOS 原生地图地理编码与反地理编码(详解)

    iOS 原生地图地理编码与反地理编码(详解)

    下面小编就为大家带来一篇iOS 原生地图地理编码与反地理编码(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • iOS实现自定义起始时间选择器视图

    iOS实现自定义起始时间选择器视图

    本篇文章主要介绍了iOS实现自定义起始时间选择器视图,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • iOS多控制器实现带滑动动画

    iOS多控制器实现带滑动动画

    这篇文章主要为大家详细介绍了iOS多控制器实现带滑动动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06

最新评论