IOS crazy explanation-tableview editing, adding, and deleting
//
// 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 @ [@ message, @ account, @ copyright]
# Define kTitleArray @ [@ receiver mode, @ Play, @ clear cache]
@ Interface VkoWLAccountVC ()
{
// Current editing mode
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: @ Account Management 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; // You can select
[Self. view addSubview: _ tableView];
Self. rightButton. hidden = NO;
Self. rightButton. backgroundColor = [UIColor clearColor];
[Self. rightButton setImage: [UIImage imageNamed: @ edit] forState :( UIControlStateNormal)];
[Self. rightButton addTarget: self action: @ selector (editButtonClick :) forControlEvents :( UIControlEventTouchUpInside)];
}
-(Void) editButtonClick :( UIButton *) button
{
[Self deleteData];
}
# Pragma mark --- UITableView return 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 --- Select to jump to the details page ---
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
}
# Pragma mark --- deit delete ---
// Make UITableView and UIViewController editable
-(Void) setEditing :( BOOL) editing animated :( BOOL) animated
{
[Super setEditing: editing animated: animated];
[_ TableView setEditing: editing animated: animated];
}
// Specify which row can be edited and which cannot be edited
-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath
{
Return YES;
}
// Set the editing button status of the row to specify the editing Style
-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath
{
Return _ editingStyle;
}
// Determine the style of the button to add or delete
-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath
{
// Delete operation
If (editingStyle = UITableViewCellEditingStyleDelete ){
[_ TitleArray removeObjectAtIndex: indexPath. row];
[_ IcoArray removeObjectAtIndex: indexPath. row];
NSArray * indexPaths = @ [indexPath]; // array of the number of rows at the index Building
// The method for deleting an index is followed by an animated style.
[_ TableView deleteRowsAtIndexPaths: indexPaths withRowAnimation :( UITableViewRowAnimationLeft)];
}
// Add operation
If (editingStyle = UITableViewCellEditingStyleInsert ){
NSArray * indexPaths = @ [indexPath];
[_ TableView insertRowsAtIndexPaths: indexPaths withRowAnimation :( UITableViewRowAnimationRight)];
}
}
# Pragma mark delete data
-(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