Ios UITableViewController (2) tableView editing mode. iostableview deletes rows.

Source: Internet
Author: User

Ios UITableViewController (2) tableView editing mode. iostableview deletes rows.

Edit mode of tableView

The table view can enter the editing mode. When you enter the editing mode, you can delete, insert, and move cells.

:

  

There are two ways to change the table view to edit mode:

The other is to set the editing attribute of tableView to enter the editing mode.

Finally, you can delete, insert, and move cells by implementing the UITableViewDataSource protocol.

 

1. In the viewDidLoad method, specify the right button in the navigation bar as the edit button.

Self. navigationItem. rightBarButtonItem = self. editButtonItem;

 

2. Another way to enter the editing mode is to modify the editing attribute of tableView.

This attribute is of the BOOL type and the default value is NO. Here, we add a left button to the navigation bar and click the left button to modify the value of the editing attribute.

Enter and exit edit mode

 

-(Void) editTableView :( UIBarButtonItem *) button

{

// Modify the value of the editing attribute to enter or exit the editing mode.

[Self. tableView setEditing :! Self. tableView. editinganimated: YES];

If (self. tableView. editing ){

Button. title = @ "done ";

}

Else {

Button. title = @ "edit ";

}

}

 

 

Delete and insert rows

Two methods and one response

Method 1: The Rows enter the editing mode.

-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath

{

Return YES;

}

 

Method 2: whether to delete or add a cell in edit mode

-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath

{

Return UITableViewCellEditingStyleDelete;

}

 

Note: In this method, two types are generally returned, and one of the default types is also returned.

Delete: UITableViewCellEditingStyleDelete

Added: UITableViewCellEditingStyleInsert

 

Response: Click the delete operation.

-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle forRowAtIndexPath :( NSIndexPath *) indexPath

{

// Delete data from the data source

[Self. array removeObjectAtIndex: indexPath. row];

// Delete the corresponding row in tableView

[TableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade];

}

Note: delete the rows in tableView in addition to the data in the data source.

 

 

Implement moving rows

When tableView enters the editing mode, each row can be moved by default, with an icon at the end of each row

A button with three rows of gray horizontal lines indicates that the row can be moved.

Method 1 response

 

Method 1: The cells can be moved.

-(BOOL) tableView :( UITableView *) tableView

CanMoveRowAtIndexPath :( NSIndexPath *) indexPath

{

Return YES;

}

 

Response: Specific mobile operations

-(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 didfinishlaunchingwitexceptions :( 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;

}

 

// Number of cell Lines

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section

{

Return self. array. count;

}

 

// Cell

-(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;

}

 

 

// Enter the editing mode for those rows and set them as needed. All the rows I set here

-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath

{

Return YES;

}

 

// Whether to delete or add a cell in edit mode

// Set it by yourself. Here I set the last cell to add and the others to delete.

-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView

EditingStyleForRowAtIndexPath :( NSIndexPath *) indexPath

{

If (indexPath. row = self. array. count-1)

Return UITableViewCellEditingStyleInsert;

Return UITableViewCellEditingStyleDelete;

}

 

// Click the delete operation to add the deletion process.

-(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];

Str2.name = @ "qwer ";

Str2.name = @" 124456 ";

[Self. array addObject: str2];

NSIndexPath * insetindexpath = [NSIndexPath indexPathForRow: self. array. count-1 inSection: 0];

[TableView insertRowsAtIndexPaths: @ [insetindexpath] withRowAnimation: UITableViewRowAnimationAutomatic];

}

}

// The cells are movable.

-(BOOL) tableView :( UITableView *) tableView

CanMoveRowAtIndexPath :( NSIndexPath *) indexPath

{

Return YES;

}

 

// Move

-(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];

Stu1.name = @ "q ";

Stu1.phone = @" 12345 ";

TRStudent * stu2 = [[TRStudent alloc] init];

Stu2.name = @ "qqw ";

Stu2.phone = @" 12345 ";

TRStudent * stu3 = [[TRStudent alloc] init];

Stu3.name = @ "sdsq ";

Stu3.phone = @" 12345 ";

NSMutableArray * mut = [[NSMutableArray alloc] init];

[Mut addObject: stu1];

[Mut addObject: stu2];

[Mut addObject: stu3];

Return mut;

}

@ End

 

Related Article

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.