iOS Uitableviewcontroller (ii) tableview edit mode

Source: Internet
Author: User

TableView's editing mode

The table view can be entered in edit mode, and can be deleted, inserted, moved and manipulated when you enter edit mode.

:

  

To get the table view into edit mode, there are two ways to enter edit mode, one is to use the navigation bar edit

button, and the other is to set the TableView editing property into edit mode.

Finally, the method of implementing Uitableviewdatasource protocol is implemented to delete, insert and move cells.

1, specify the right button of the navigation bar as the Edit button in the Viewdidload method

Self.navigationItem.rightBarButtonItem =self.editbuttonitem;

2, another way to enter edit mode is to modify the TableView editing property

This property is a bool type, the default value is no, here a left button is added to the navigation bar, and the value of the editing property is modified by clicking the left button

Enter and exit edit mode

-(void) Edittableview: (Uibarbuttonitem *) button

{

Modify the value of the editing property to enter or exit edit mode

[Self.tableview Setediting:!self.tableview.editinganimated:yes];

if (self.tableView.editing) {

Button.title = @ "Done";

}

else{

Button.title = @ "edit";

}

}

Implementing Delete and Insert rows

Two-way One response

Method One: Those lines go into edit mode

-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath

{

return YES;

}

Method Two: The cell that enters edit mode is deleted or incremented

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath

{

return uitableviewcelleditingstyledelete;

}

Note: Two types are generally returned in this method, as well as one of the default types

Delete: Uitableviewcelleditingstyledelete

Added: Uitableviewcelleditingstyleinsert

Response: Click the delete process that executes when you click Delete

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath

{

Delete data from the data source

[Self.array RemoveObjectAtIndex:indexPath.row];

Then delete the corresponding row in the TableView

[TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];

}

Note: In addition to the data in the data source, delete the corresponding row in the TableView

Implementing Moving Rows

When TableView enters edit mode, each row is movable by default, with an icon at the end of each line

is a three-line gray horizontal button that indicates that the row can be moved

One method One Response

Method One: Those cells can move

-(BOOL) TableView: (UITableView *) TableView

Canmoverowatindexpath: (Nsindexpath *) Indexpath

{

return YES;

}

Response: Move the specific action

-(void) TableView: (UITableView *) TableView

Moverowatindexpath: (Nsindexpath *) Fromindexpath

Toindexpath: (Nsindexpath *) Toindexpath

{

Trstudent *stu=self.array[fromindexpath.row];

[Self.array RemoveObjectAtIndex:fromIndexPath.row];

[Self.array InsertObject:stuatIndex:toIndexPath.row];

}

Case

AppDelegate.h

#import <UIKit/UIKit.h>

@interface Appdelegate:uiresponder <UIApplicationDelegate>

@property (Strong, nonatomic) UIWindow *window;

@end

Appdelegate.m

#import "AppDelegate.h"

#import "TRStudent.h"

#import "TRTableViewController.h"

@implementation Appdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

{

Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];

Override point for customization after application launch.

Self.window.backgroundColor = [Uicolor Whitecolor];

Trtableviewcontroller *tr1=[[trtableviewcontroller alloc] initwithnibname:@ "Trtableviewcontroller" bundle:nil];

Uinavigationcontroller *navi=[[uinavigationcontroller alloc] initwithrootviewcontroller:tr1];

Self.window.rootviewcontroller=navi;

[Self.window makekeyandvisible];

return YES;

}

@end

TRTableViewController.h

#import <UIKit/UIKit.h>

@interface Trtableviewcontroller:uitableviewcontroller

@property (Nonatomic,strong) Nsmutablearray *array;

@end

Trtableviewcontroller.m

#import "TRTableViewController.h"

#import "TRStudent.h"

@interface Trtableviewcontroller ()

@end

@implementation Trtableviewcontroller

-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil

{

return self=[super Initwithnibname:nibnameornil Bundle:nibbundleornil];

}

-(void) viewdidload

{

[Super Viewdidload];

Self.array=[trstudent GetArray];

Self.navigationitem.rightbarbuttonitem=self.editbuttonitem;

}

How many partitions are there?

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView

{

return 1;

}

How many cell units are there?

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

return self.array.count;

}

Cell Unit

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cell"];

if (Cell==nil)

{

Cell=[[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:@ "Cell"];

}

Trstudent *stu=self.array[indexpath.row];

Cell.textlabel.text=stu.name;

return cell;

}

Those lines into the edit mode, according to your needs to set their own, I set here all

-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath

{

return YES;

}

Whether the cell entering edit mode is deleted or incremented

self-setting, I set here is the last cell cell is incremented, the other is deleted

-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView

Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath

{

if (indexpath.row==self.array.count-1)

return uitableviewcelleditingstyleinsert;

return uitableviewcelleditingstyledelete;

}

Click Delete to perform the add process when you click Delete

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath

{

if (Editingstyle = = Uitableviewcelleditingstyledelete) {

[Self.array RemoveObjectAtIndex:indexPath.row];

[TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];

} else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {

Trstudent *str2=[[trstudent alloc] init];

[Email protected] "qwer";

[Email protected] "124456";

[Self.array ADDOBJECT:STR2];

Nsindexpath *insetindexpath=[nsindexpath indexpathforrow:self.array.count-1 insection:0];

[TableView Insertrowsatindexpaths:@[insetindexpath] withrowanimation:uitableviewrowanimationautomatic];

}

}

those cells can move .

-(BOOL) TableView: (UITableView *) TableView

Canmoverowatindexpath: (Nsindexpath *) Indexpath

{

return YES;

}

Moving

-(void) TableView: (UITableView *) TableView

Moverowatindexpath: (Nsindexpath *) Fromindexpath

Toindexpath: (Nsindexpath *) Toindexpath

{

Trstudent *stu=self.array[fromindexpath.row];

[Self.array RemoveObjectAtIndex:fromIndexPath.row];

[Self.array InsertObject:stuatIndex:toIndexPath.row];

}

@end

TRStudent.h

#import <Foundation/Foundation.h>

@interface Trstudent:nsobject

@property (Nonatomic,strong) NSString *name;

@property (Nonatomic,strong) NSString *phone;

+ (Nsmutablearray *) GetArray;

@end

Trstudent.m

#import "TRStudent.h"

@implementation Trstudent

+ (Nsmutablearray *) GetArray

{

Trstudent *stu1=[[trstudent alloc] init];

[Email protected] "q";

[Email protected] "12345";

Trstudent *stu2=[[trstudent alloc] init];

[Email protected] "QQW";

[Email protected] "12345";

Trstudent *stu3=[[trstudent alloc] init];

[Email protected] "SDSQ";

[Email protected] "12345";

Nsmutablearray *mut=[[nsmutablearray alloc] init];

[Mut ADDOBJECT:STU1];

[Mut ADDOBJECT:STU2];

[Mut ADDOBJECT:STU3];

return mut;

}

@end

iOS Uitableviewcontroller (ii) tableview edit mode

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.