当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS UITableView 拖动排序实现代码

iOS UITableView 拖动排序实现代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

母语负迁移,宜配网,福建莆田一中

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网