Tableviewcontroller Add, delete, move

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.