iOS瘋狂詳解之tableview編輯添加刪除

來源:互聯網
上載者:User

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

 

 

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.