iOS开发之高级视图—— UITableView操作——移动

      移动操作步骤:

            1:需要先设置
         //设置UITableViewDelegate 代理
                 tableView.delegate = self;
            2:设置tableView允许编辑操作:
                - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
                //开启编辑模式
                        [tableView setEditing:YES animated:YES];
               }
            3:设置 (可选)
       - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;方法
            4:设置 (可选)
        //指定允许移动的Cell ,通常不用实现,不实现就表示全部Cell支持移动操作

                -(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:                                                           (NSIndexPath *)proposedDestinationIndexPath;

           5:实现
              //移动位置操作
              - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
                id targetObj = [list objectAtIndex:sourceIndexPath.row];
                       [list insertObject:targetObj atIndex:destinationIndexPath.row];

             }

         下面是一个实现了UITableView的行移动操作的例子

         ViewController.m

//
//  ViewController.m
//  UITableViewMoveApp
//
//  Created by Apple on 16/5/25.
//  Copyright © 2016年 Apple. All rights reserved.
//

//
//  ViewController.m
//  UITableViewMoveApp
//
//  Created by apple on 15/6/6.
//  Copyright (c) 2015年 fkit.org. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

NSMutableArray* list;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view setBackgroundColor:[UIColor redColor]];
    
    //创建一个数组,存储需要显示的数据
    // 初始化NSMutableArray集合
    list = [[NSMutableArray alloc] initWithObjects:@"李青",
            @"瑞兹",
            @"扎克",
            @"卡利斯塔",
            @"提莫",
            @"阿狸",nil];
    
    //创建UITableView对象
    UITableView* tableView =  [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
    
    // 设置数据源代理,必须实现协议UITableViewDataSource中的相关方法
    tableView.dataSource = self;
    tableView.delegate = self;
    
    UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 50)];
    [headerLabel setText:@"英雄列表"];
    //设置UITable头信息
    [tableView setTableHeaderView:headerLabel];
    
    UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    [footerLabel setText:@"申请出战"];
    //设置UITable尾部信息
    [tableView setTableFooterView:footerLabel];
    
    //设置行cell高(默认44px)
    [tableView setRowHeight:50];
    //设置分割线颜色
    [tableView setSeparatorColor:[UIColor redColor]];
    //设置分割线风格
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    
    UIImageView* iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg.jpg"]];
    //设置背景
    [tableView setBackgroundView:iv];
    
    [self.view addSubview:tableView];
    
}

#pragma mark -UITableViewDataSource

// @required
//提供tableView中的分区中的数据的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [list count];
}


//可重用标识符,在UITableView的cell缓存池当中所有的cell的标示符都是刚定义的cellID,因为重用时无所谓获取哪一个cell,只要是cell就可以
static NSString* cellID = @"cellID";

// @required
//将提供 tableView 中显示的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 根据cellID获取可重用的UITableViewCell对象
    UITableViewCell* tableViewCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (tableViewCell == nil) {
        //创建一个UITableViewCell对象,并绑定到cellID
        tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    //设置cell上显示的数据
    tableViewCell.textLabel.text = [list objectAtIndex:indexPath.row];
    //设置 选择 图标
    tableViewCell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    //设置 选中 cell的状态
    tableViewCell.selectionStyle = UITableViewCellSelectionStyleBlue;
    //返回设置好数据的cell给UITableView对象
    return tableViewCell;
    
    
}

// @optional

//设置表视图的分区数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//设置区域的名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"移动英雄";
}

#pragma mark -------UITableViewDelegate

//通过本方法返回删除(UITableViewCellEditingStyleDelete)或者新增(UITableViewCellEditingStyleInsert);
//若不实现此方法,则默认为删除模式。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"-----editingStyleForRowAtIndexPath------");
    return UITableViewCellEditingStyleDelete;
    
}

// 设置点击某个Cell的响应操作,选择某行的时候执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"--您选中的数据是-[%@]---",[list objectAtIndex:indexPath.row]);
    //设置tableView允许进入编辑状态(默认的编辑状态是 删除状态)
    [tableView setEditing:YES animated:YES];
}


// UITableViewDataSource协议中定义的方法。移动完成时激发该方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSLog(@"-----move-----");
    //判断fromIndexPath 是否和toIndexPath相同,相同表示不用移动
    if (sourceIndexPath == destinationIndexPath) {
        return;
    }
    NSLog(@"-----sourceIndexPath=%ld-----",sourceIndexPath.row);
    // 获取将要移动的数据
    id targetObj = [list objectAtIndex:sourceIndexPath.row];
    NSLog(@"-----%@-----",targetObj);
    // 从底层数组中删除指定数据项
    [list removeObjectAtIndex:sourceIndexPath.row];
    NSLog(@"-----destinationIndexPath=%ld-----",destinationIndexPath.row);
    // 将移动的数据项插入到指定位置。
    [list insertObject:targetObj atIndex:destinationIndexPath.row];
    
}

@end

      效果图如下:

       启动页面如下:


   点击任一行进入如下画面

  

     鼠标按住右边的三横线标记,拖动可以交换行的位置

   

       可以点击左侧“➖”号,发现是删除模式,此为默认模式,但是点击删除无反应,因为代码并没有实现删除的方法

  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值