Methods and notes for deleting multiple rows in TableView editing. Multiple rows in tableview

Source: Internet
Author: User

Methods and notes for deleting multiple rows in TableView editing. Multiple rows in tableview

In the last learning and editing of tableView, how to delete, add, and move a row is implemented through the proxy method, for beginners, note that the root data must be consistent with the data in tablView when editing. Because each time a tableView is modified, the data in tableView is refreshed. In short, the data in an array is deleted and a row in tableView is deleted;

After ios5.0:

The table view adds a new function to multiple options. Its default value is NO self. tableView. allowsMultipleSelectionDuringEditing = YES; he generally writes the tableView editing style.

// The selected cell index set NSArray * indexpath = self. tableView. indexPathsForSelectedRows; with these two methods, the logic complexity can be greatly reduced:

// Create a UIBarButtonItrm instance and implement a response event method.

UIBarButtonItem * barItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @ selector (Cancel)];

Self. navigationItem. leftBarButtonItem = barItem;

-(Void) Cancel {

// Put the index set of the number of rows that the user hooks into an array

NSArray * indexpath = self. tableView. indexPathsForSelectedRows;

NSLog (@ "% @", indexpath );

For (int I = 0; I <indexpath. count; I ++ ){

// Traverse this array and retrieve each element

NSNumber * number = [mulArray objectAtIndex: I];

Int a = [number intValue];

NSString * str = [mulArray objectAtIndex: a];

Then, the elements retrieved are deleted from the array. This is what we mentioned above. to edit tableView, we need to change the root data first.

[MulArray removeObject: str];

}

If I had to use the online method before ios5.0, I should first say that a readable or comments must be added when I publish my own code, these codes have made me a lot of effort for beginners.

Actually, a combined proxy method is used # import "UITableViewDelteMutilRowsViewController. h"

@ Implementation progress @ synthesize tableview; @ synthesize dataArray; @ synthesize deleteDic; @ synthesize leftButton; @ synthesize rightButton; # pragma mark-# pragma mark View lifecycle-(void) viewDidLoad {[super viewDidLoad];
// Create a data record and a dictionary, and name the Custom button dataArray = [[NSMutableArray alloc] initWithObjects: @ "1", @ "2 ", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", nil]; deleteDic = [[NSMutableDictionary alloc] init]; rightButton. title = @ "edit";}-(IBAction) choseData {
// When this button is clicked, It is triggered to determine whether it is in the editing status. If it is not the name of the button, the editing status is enabled. If (rightButton. title = @ "edit") {rightButton. title = @ "OK"; [self. tableview setEditing: YES animated: YES];} else {rightButton. title = @ "edit"; [deleteDic removeAllObjects]; [self. tableview setEditing: NO animated: YES] ;}}-(IBAction) deleteFuntion {
// This button deletes all data from the array and tableView.
[DataArray removeObjectsInArray: [deleteDic allKeys]; [self. tableview attributes: [NSArray arrayWithArray: [deleteDic allValues] withRowAnimation: Attributes]; [deleteDic attributes];}-(void) dealloc {[leftButton release]; [rightButton release]; [deleteDic release]; [dataArray release]; [tableview release]; [super dealloc] ;}# pragma mark-# pragma mark Table view da Ta source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {// Return the number of sections. return 1;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// Return the number of rows in the section. return [dataArray count];} // Customize the appearance of table view cells. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtI NdexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "Cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];} // Configure the cell... cell. textLabel. text = [dataArray objectAtIndex: indexPath. row]; retur N cell;}/* // here it is set to slide edit and delete // Override to support conditional editing of the table view. -(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {// Return NO if you do not want the specified item to be editable. return YES;} */-(UITableViewCellEditingStyle) tableView :( UITableView *) tableView editingStyleForRowAtIndexPath :( NSIndexPath *) indexPath {return UITableVi EwCellEditingStyleDelete | UITableViewCellEditingStyleInsert;} # pragma mark-# pragma mark Table view delegate // these two proxy methods are used in combination when you click tableView, this dictionary can extract the number of lines clicked by the user in the root data of the array and assign the Value of the key to the dictionary. The path is the Value-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {if (rightButton. title = @ "OK") {[deleteDic setObject: indexPath forKey: [dataArray objectAtIndex: indexPath. ro W] ;}else {}// this method is called when editing is canceled and the key value of the corresponding dictionary is deleted. This is deselected. -(Void) tableView :( UITableView *) tableView didDeselectRowAtIndexPath :( NSIndexPath *) indexPath {if (rightButton. title = @ "OK") {[deleteDic removeObjectForKey: [dataArray objectAtIndex: indexPath. row] ;}}@ 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.