#import "RootTableViewController.h"
@interface roottableviewcontroller ()
{
Uitableviewcelleditingstyle _style;
}
@property(nonatomic, strong)Nsmutablearray *array;
@end
@implementation Roottableviewcontroller
-(ID) Initwithstyle: (uitableviewstyle) style
{
Self = [super Initwithstyle:style];
if (self) {
Custom initialization
self. Array=[nsmutablearray array];
}
return self;
}
-(void) Viewdidload
{
[Super Viewdidload];
//Save some data in the array
For (int i=0; i<3; i++) {
Nsmutablearray *temparray=[nsmutablearray array];
For (int j=0; j<5; j + +) {
[Temparray addobject:[nsstring stringwithformat:@ " Group%d , section %d Individual ", i,j]];
}
[Self. array Addobject:temparray];
}
Set up proxy
self. TableView. DataSource=self;
self. TableView. Delegate=self;
add two Barbutton on both sides of the navigation bar
Uibarbuttonitem *bar=[[uibarbuttonitem alloc] initwithtitle:@ edit style:( Uibarbuttonitemstyledone) Target:self Action:@selector(baraction:)];
self. Navigationitem. Leftbarbuttonitem=bar;
Uibarbuttonitem *bar1=[[uibarbuttonitem alloc] initwithtitle:@ " add " style:( Uibarbuttonitemstyledone) Target:self Action:@selector(barAction1:)];
self. Navigationitem. Rightbarbuttonitem=bar1;
}
Barbutton Editing Events
-(void) Baraction: (uibarbuttonitem *) Sender
{
_style=uitableviewcelleditingstyledelete;
BOOL flag=self. TableView. editing;
[self. TableView setediting:!flag animated:YES];
}
Barbutton Adding events
-(void) BarAction1: (uibarbuttonitem *) Sender
{
_style=uitableviewcelleditingstyleinsert;
BOOL flag=self. TableView. editing;
[self. TableView setediting:!flag animated:YES];
}
Set which lines can be edited (all by default )
-(BOOL) TableView: (uitableview *) TableView Caneditrowatindexpath: (nsindexpath *) Indexpath
{
return YES;
}
Determine Edit Status (delete | add )
-(Uitableviewcelleditingstyle) TableView: (uitableview *) TableView Editingstyleforrowatindexpath: ( Nsindexpath *) Indexpath
{
return _style;
}
Submit edits
-(void) TableView: (uitableview *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (nsindexpath *) indexpath
{
if(editingstyle==uitableviewcelleditingstyledelete)
{
// Delete
[Self. Array[indexpath. Section]Removeobjectatindex:indexpath. Row];
[TableView deleterowsatindexpaths:@[indexpath] withrowanimation:( Uitableviewrowanimationleft)];
}
Else
{
// add
nsstring *s=@ " test data ";
[Self. Array[indexpath. Section]insertobject:s atindex:indexpath. row+1];
Nsindexpath *index=[nsindexpath indexpathforrow: Indexpath.row+1 insection: Indexpath.section];
[TableView insertrowsatindexpaths:@[index] withrowanimation:( Uitableviewrowanimationright)];
}
}
Which rows can be moved
-(BOOL) TableView: (uitableview *) TableView Canmoverowatindexpath: (nsindexpath *) Indexpath
{
return YES;
}
Complete the move
-(void) TableView: (uitableview *) TableView Moverowatindexpath: (nsindexpath *) Sourceindexpath Toindexpath: (nsindexpath *) destinationindexpath
{
Assigning values First
ID obj= self. Array[sourceindexpath. Section][sourceindexpath. Row];
then delete
[Self. Array[sourceindexpath. Section] Removeobjectatindex:sourceindexpath. Row];
last added
[Self. Array[destinationindexpath. Section]insertobject:obj atindex:destinationindexpath. Section];
}
Check out of Bounds
-(Nsindexpath *) TableView: (uitableview *) TableView Targetindexpathformovefromrowatindexpath: ( Nsindexpath *) Sourceindexpath Toproposedindexpath: (nsindexpath *) Proposeddestinationindexpath
{
if (Sourceindexpath. Section!=proposeddestinationindexpath. Section )
{
return sourceindexpath;
}
Else
{
return proposeddestinationindexpath;
}
}
Tableviewcontroller Add, delete, move