码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-01-15 16:12:44      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:ios开发   tableview   editing   

//
//  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

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

标签:ios开发   tableview   editing   

原文地址:http://blog.csdn.net/wanglongblog/article/details/42740623

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!