iOS开发之高级视图—— UITableView操作——删除

     删除操作步骤:

         1:先设置UITableView代理
               //设置UITableViewDelegate 代理
               tableview.delegate = self;
         2:设置tableView允许编辑:
               - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
                 //开启编辑模式
                         [tableView setEditing:YES animated:YES];
              }
        3. 设置行是否可以编辑
            // UITableViewDataSource协议中定义的方法。该方法的返回值决定某行是否可编辑
           - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
                  return YES;
           }

       4:设置 (可选)

             - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;方法

             //通过本方法返回删除(UITableViewCellEditingStyleDelete)或者新增(UITableViewCellEditingStyleInsert)状态;
             //若不实现此方法,则默认为删除模式。
            - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
                       //表示支持默认操作
                       //return UITableViewCellEditingStyleNone;
                      //表示支持删除操作
                      //return UITableViewCellEditingStyleDelete;
                     //表示支持新增操作
                     return UITableViewCellEditingStyleInsert;
            }
       5:进行删除操作
              -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
                      // 删除数组中的对应数据,注意cityList 要是可变的集合才能够进行删除或新增操作
                      [list removeObjectAtIndex:indexPath.row];
                      //tableView刷新方式   设置tableView带动画效果 删除数据
                      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
                      //取消编辑状态
                     [tableView setEditing:NO animated:YES];

             }

 

     

       下面是一个实现UITableView的行删除操作的例子

        ViewController.m

//
//  ViewController.m
//  UITableViewDeleteApp
//
//  Created by Apple on 16/5/25.
//  Copyright © 2016年 Apple. 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];
    
    [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 @"删除英雄";
}

// UITableViewDelegate协议中定义的方法。
// 该方法的返回值作为删除指定表格行时确定按钮的文本
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:
(NSIndexPath *)indexPath{
    return @"删除";
}

// UITableViewDataSource协议中定义的方法。该方法的返回值决定某行是否可编辑
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:
(NSIndexPath *)indexPath
{
    // 如果该表格行的数据为提莫,返回NO——代表这行数据不能编辑
    if ([[list objectAtIndex:[indexPath row]] isEqualToString:@"提莫"])
    {
        NSLog(@"-----提莫不能删除------");
        return NO;
    }
    // 除了第2个表格行的数据不能编辑
    if (indexPath.row == 1) {
        NSLog(@"-----第二行不能删除------");
        return NO;
    }
    return YES;
}

//设置具体的编辑操作(新增,删除)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"-----删除------%ld",indexPath.row);
    删除数组中的对应数据,注意list 要是可变的集合才能够进行删除或新增操作
    [list removeObjectAtIndex:indexPath.row];
    
    // tableView刷新方式1    重新加载tableView,没有动画效果
    // [tableView reloadData];
    
    //tableView刷新方式2    设置tableView带动画效果 删除数据
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
    
    //取消编辑状态
    [tableView setEditing:NO animated:YES];
    
}

#pragma mark -------UITableViewDelegate

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

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

@end

       效果图如下:

  

      点击任一行进入如下画面,第一行的“李青”和名字为“提莫”的行不能删除,与代码设置的相符。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值