iOS UITableView 拖动排序实现代码_IOS

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

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ios
, uitableview
, 排序
拖动
php实现拖动互换排序、ios 长按拖动排序效果、ios 长按图片拖动排序、ios拖动排序、快速排序算法实现代码,以便于您获取更多的相关知识。

时间: 2024-08-11 23:13:51

iOS UITableView 拖动排序实现代码_IOS的相关文章

jQuery仿360导航页图标拖动排序效果代码分享_jquery

jquery实现360浏览器导航页图标拖动从新排序特效源码是一款模仿360浏览器导航页网站图标拖动排序的代码.本段代码适应于所有网页使用,有兴趣的朋友们可以学习一下. 运行效果图:                                         ----------------------查看效果 下载源码-----------------------   小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的360导航页图标拖动排序效果代码如下 <!DOCTYP

iOS AFNetworking中cookie重定向代码_IOS

// 1. 取出需要同步的url (登录请求中返回的重定向地址) BESTHttpItem *httpItem = [BESTHttpHelper sharedHelper].curHttpItem; NSString *url = [NSString stringWithFormat:@"%@/#/login", httpItem.frontend_addr]; // 2. 取出当前的headerFields NSDictionary *headerFields = [NSHTTPC

iOS异步下载图片实例代码_IOS

写在前面 在iOS开发中,无论是在UITableView还是在UICollectionView中,通过网络获取图片设置到cell上是较为常见的需求.尽管有很多现存的第三方库可以将下载和缓存功能都封装好了供开发者使用,但从学习的角度出发,看懂源码,理解其中的原理,结合自身的实际需求写出自己的代码是很必要的.在刚结束的Demo中,有用到异步图片下载功能,这篇笔记就是对整个实现的简单整理. 基本思路 •cell中添加一个UIImageView •cell拥有url,发起下载请求,注册下次完成通告,在通

IOS 缓存文件的清除实现代码_IOS

移动互联网 APP 的应用开发,必须要时刻注意用户体验,以免造成APP 或者手机及其他移动设备的卡死情况,以下是对缓存文件的处理. 移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类app的标配功能. 今天介绍的离线缓存功能的实现,主要分为缓存文件大小的获取.清除缓存文件的实现. 1. 获取缓存文件的大小 -( float )readCacheSi

iOS获取手机ip地址代码_IOS

本文实例为大家分享了iOS获取手机ip地址的具体代码,供大家参考,具体内容如下 #import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int s

iOS UITableView 与 UITableViewController实例详解_IOS

很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名. UITableView 对象虽然只能显示一行数据,但是没有行数限制. •编写新的应用程序 JXHomepwner 应用 创建应用,填写基本信息 •UITableViewController UITableView 是视图.我们知道 模型-视图-控制器(Model-View-Controller),他是我们必须遵守的一种

举例讲解iOS开发中拖动视图的实现_IOS

预备知识iOS处理屏幕上的触摸动作,主要涉及到以下几个方法: 复制代码 代码如下: touchesBegan:withEvent:          //触摸屏幕的最开始被调用 touchesMoved:withEvent:         //移动过程中被调用 touchesEnded:withEvent:         //动作结束时被调用 touchesCancelled:WithEvent: 从方法的命名可以清晰的看出该方法何时被调用,最后一个比较特殊.touchesCancelled

ios系统下删除文件的代码_IOS

方法一:这段objective c代码用于删除指定路径的文件 if ([fileManager removeItemAtPath:@"FilePath" error:NULL]) { NSLog(@"Removed successfully"); } 方法二: NSFileManager *defaultManager; defaultManager = [NSFileManager defaultManager]; [defaultManager removeFi

IOS 改变键盘颜色代码_IOS

IOS 改变键盘颜色的代码 iPhone和iPod touch的键盘颜色其实是可以通过代码更改的,这样能更匹配App的界面风格,下面是改变iPhone键盘颜色的代码. 1.只有Number Pad和Phone Pad这两种数字键盘才有效果 2.设置Appearance为Alert 复制代码 代码如下: - (void)textFieldDidBeginEditing:(UITextField *)textField{     NSArray *ws = [[UIApplication share