最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS UITableView 拖動排序?qū)崿F(xiàn)代碼

 更新時(shí)間:2016年09月27日 10:23:06   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了iOS UITableView 拖動排序?qū)崿F(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

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

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

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

3、實(shí)現(xiàn)tableView:moveRowAtIndexPath:toIndexPath方法,只要實(shí)現(xiàn)該方法,就能實(shí)現(xiàn)單元格的拖動排序,但只是實(shí)現(xiàn)了表面的排序,并沒有修改真實(shí)地?cái)?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 點(diǎn)擊編輯按鈕
- (IBAction)clickEditBtn:(UIButton *)sender {

  //設(shè)置tableview編輯狀態(tài)
  BOOL flag = !_tableView.editing;
  [_tableView setEditing:flag animated:YES];
  _editBtn.selected = flag;
}

#pragma mark 選擇編輯模式,添加模式很少用,默認(rèn)是刪除
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return UITableViewCellEditingStyleNone;
}

#pragma mark 排序 當(dāng)移動了某一行時(shí)候會調(diào)用
//編輯狀態(tài)下,只要實(shí)現(xiàn)這個(gè)方法,就能實(shí)現(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵詳解

    如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵詳解

    xcode是蘋果公司向開發(fā)人員提供的集成開發(fā)環(huán)境,開發(fā)者們經(jīng)常會使用到,下面這篇文章主要給大家介紹了關(guān)于如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵的相關(guān)資料,需要的朋友可以參考下。
    2018-04-04
  • IOS 中CATextLayer繪制文本字符串

    IOS 中CATextLayer繪制文本字符串

    這篇文章主要介紹了IOS 中CATextLayer繪制文本字符串的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • iOS如何獲取當(dāng)前日期前后N天的時(shí)間示例代碼

    iOS如何獲取當(dāng)前日期前后N天的時(shí)間示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS如何獲取當(dāng)前日期前后N天的時(shí)間的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。
    2017-11-11
  • iOS當(dāng)多個(gè)網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解

    iOS當(dāng)多個(gè)網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解

    在多線程中,有時(shí)候我們會遇到一個(gè)界面同時(shí)有多個(gè)網(wǎng)絡(luò)請求(比如a,b,c,d四個(gè)網(wǎng)絡(luò)請求),在這四個(gè)個(gè)請求結(jié)束后,在請求到數(shù)據(jù)去做其他操作(UI更新等),下面這篇文章主要給大家介紹了關(guān)于iOS當(dāng)多個(gè)網(wǎng)絡(luò)請求完成后執(zhí)行下一步的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • IOS客戶端接入微信支付

    IOS客戶端接入微信支付

    對于一個(gè)ios的app,如果有一些虛擬的商品或者服務(wù)需要通過在線支付來收費(fèi)的話,一般有幾種主流的選擇。如果是通過APP調(diào)用支付平臺APP的思路的話,一個(gè)是調(diào)起支付寶客戶端,一個(gè)則是調(diào)起微信支付。本文給大家分享ios客戶端接入微信支付,需要的朋友可以參考下
    2015-09-09
  • swift3.0網(wǎng)絡(luò)圖片緩存原理簡析

    swift3.0網(wǎng)絡(luò)圖片緩存原理簡析

    這篇文章主要為大家簡析了swift3.0網(wǎng)絡(luò)圖片緩存原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • iOS關(guān)鍵字static extern const使用示例詳解

    iOS關(guān)鍵字static extern const使用示例詳解

    這篇文章主要為大家介紹了iOS關(guān)鍵字static extern const使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • iOS 原生地圖地理編碼與反地理編碼(詳解)

    iOS 原生地圖地理編碼與反地理編碼(詳解)

    下面小編就為大家?guī)硪黄猧OS 原生地圖地理編碼與反地理編碼(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖

    iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖

    本篇文章主要介紹了iOS實(shí)現(xiàn)自定義起始時(shí)間選擇器視圖,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • iOS多控制器實(shí)現(xiàn)帶滑動動畫

    iOS多控制器實(shí)現(xiàn)帶滑動動畫

    這篇文章主要為大家詳細(xì)介紹了iOS多控制器實(shí)現(xiàn)帶滑動動畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06

最新評論

罗平县| 合川市| 桃江县| 盐城市| 永清县| 南靖县| 鹤庆县| 平遥县| 绥化市| 长泰县| 宁津县| 杨浦区| 乌审旗| 定州市| 永和县| 黄大仙区| 永吉县| 赤峰市| 沾益县| 万宁市| 东安县| 花垣县| 枞阳县| 玛多县| 定安县| 额尔古纳市| 五常市| 桑日县| 钟山县| 翼城县| 珠海市| 丰原市| 古浪县| 康定县| 安国市| 蒙阴县| 盐津县| 郎溪县| 宁南县| 元氏县| 新干县|