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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。