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