iOS疯狂详解之tableview编辑添加删除

//
//  VkoWLAccountVC.m
//  PocketUniversity
//
//  Created by long on 15-1-14.
//  Copyright (c) 2015年 WLong. All rights reserved.
//

#import "VkoWLAccountVC.h"
#import "VkoWLMoreTableViewCell.h"

#define kIcoArray @[@"消息",@"账号",@"版权"]
#define kTitleArray @[@"听筒模式",@"播放",@"清除缓存"]

@interface VkoWLAccountVC ()
{
    // 当前的编辑模式
    UITableViewCellEditingStyle _editingStyle;
    NSMutableArray *_icoArray;
    NSMutableArray *_titleArray;
}
@end

@implementation VkoWLAccountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _icoArray = [NSMutableArray arrayWithArray:kIcoArray];
    _titleArray = [NSMutableArray arrayWithArray:kTitleArray];

    
    [self navTitle:@"账号管理" leftButtonHidden:NO];
    
    [self createView];
    
    _editingStyle = UITableViewCellEditingStyleDelete;

}

- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.bgImageView.bottom, kUIScreenWidth, [self getMiddleViewHight]) style:(UITableViewStyleGrouped)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsSelectionDuringEditing = YES;  //编辑状态下可以选中
    [self.view addSubview:_tableView];
    
    
    self.rightButton.hidden = NO;
    self.rightButton.backgroundColor = [UIColor clearColor];
    [self.rightButton setImage:[UIImage imageNamed:@"编辑"] forState:(UIControlStateNormal)];
    [self.rightButton addTarget:self action:@selector(editButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)editButtonClick:(UIButton *)button
{
    [self deleteData];
}


#pragma mark --- UITableView 返回Cell ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"VkoWLMoreTableViewCell";
    
    VkoWLMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        
        cell = [[VkoWLMoreTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellIdentifier] ;
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.iocImageView.image = [UIImage imageNamed:_icoArray[indexPath.row]];
    cell.labelTitle.text = _titleArray[indexPath.row];
    [cell.iocImageView.layer setMasksToBounds:YES];
    [cell.iocImageView.layer setCornerRadius:25 / 2];
    cell.arrowImageView.hidden = YES;

    
    return cell;
    
}

#pragma mark ---tableView dataSource---
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 20+25;
}

#pragma mark --- 选中跳转 到详情界面 ---
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark ---deit delete---
// 让 UITableView 和 UIViewController 变成可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [_tableView setEditing:editing animated:animated];
}

//  指定哪一行可以编辑 哪行不能编辑
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//  设置 哪一行的编辑按钮 状态 指定编辑样式
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _editingStyle;
}

// 判断点击按钮的样式 来去做添加 或删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除的操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [_titleArray removeObjectAtIndex:indexPath.row];
        [_icoArray removeObjectAtIndex:indexPath.row];

        
        NSArray *indexPaths = @[indexPath]; // 构建 索引处的行数 的数组
        // 删除 索引的方法 后面是动画样式
        [_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationLeft)];
        
    }
    
    // 添加的操作
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        NSArray *indexPaths = @[indexPath];
        [_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationRight)];
        
    }
    
}

#pragma mark 删除数据
- (void)deleteData
{
    _editingStyle = UITableViewCellEditingStyleDelete;
    
    BOOL isEditing = self.tableView.isEditing;
    
    [self.tableView setEditing:!isEditing animated:YES];
}

- (void)addData
{
    _editingStyle = UITableViewCellEditingStyleInsert;
    
    BOOL isEditing = self.tableView.isEditing;
    
    [self.tableView setEditing:!isEditing animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




更多iOS疯狂详解:http://blog.csdn.net/wanglongblog

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值