IOS UItableView editable

Source: Internet
Author: User

IOS UItableView editable

//

// RootViewController. m

// TableViewEdit

//

// Created by Duba on 15-5-07.

// Copyright (c) 2015 Dubai. All rights reserved.

//

 

# Import "RootViewController. h"

 

@ Interface RootViewController ()

 

{

 

 

NSMutableArray * _ dataArray;

UITableView * _ tableView;

NSMutableArray * _ arr1;

 

}

@ End

 

@ Implementation RootViewController

 

-(Void) dealloc

{

[_ DataArray release];

[_ TableView release];

[Super dealloc];

}

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];

If (self ){

// Custom initialization

}

Return self;

}

 

-(Void) viewDidLoad

{

[Super viewDidLoad];

// Do any additional setup after loading the view.

 

// Asia

_ Arr1 = [NSMutableArray arrayWithObjects: @ "China", @ "Singapore", @ "Mongolia", nil];

// Europe

NSMutableArray * arr2 = [NSMutableArray arrayWithObjects: @ "USA", @ "Canada", @ "Brazil", nil];

 

// Europe

NSMutableArray * arr3 = [NSMutableArray arrayWithObjects: @ "UK", @ "France", @ "Germany", nil];

 

// Add a small array of three States to a large number Group

_ DataArray = [[NSMutableArray alloc] initWithObjects: _ arr1, arr2, arr3, nil];

 

//// Add a button

// UIBarButtonItem * edite = [[UIBarButtonItem alloc] initWithTitle: @ "edit" style :( UIBarButtonItemStylePlain) target: self action: @ selector (didClickEditButtonitem :)];

// Self. navigationItem. rightBarButtonItem = edite;

// [Edite release];

 

 

// Use the built-in controller to edit the buttonitem.

Self. navigationItem. rightBarButtonItem = self. editButtonItem; // contains an attribute.

 

_ TableView = [[UITableView alloc] initWithFrame: self. view. bounds style :( UITableViewStyleGrouped)];

[Self. view addSubview: _ tableView];

_ TableView. dataSource = self;

_ TableView. delegate = self;

_ TableView. separatorColor = [UIColor greenColor];

// Self. editButtonItem. title = @ "edit ";

 

}

 

// Controller rewrite Method

-(Void) setEditing :( BOOL) editing animated :( BOOL) animated

{

[Super setEditing: editing animated: animated];

// NSLog (@ "% d", editing );

[_ TableView setEditing: editing animated: animated];

 

Self. editButtonItem. title = editing? @ "Complete": @ "edit ";

 

}

 

 

//// Response to the button and enter the editing status

//-(Void) didClickEditButtonitem :( UIBarButtonItem *) buttonitem

//{

//

/// TableView enters the editing status

// If ([buttonitem. title isEqualToString: @ "edit"]) {

// [_ TableView setEditing: YES animated: YES];

// Buttonitem. title = @ "done ";

//} Else {

//

// [_ TableView setEditing: NO animated: YES];

// Buttonitem. title = @ "edit ";

//

//}

/// [_ TableView setEditing: YES animated: YES];

//

//

//

//}

 

// Set which rows of tableView can be edited. The default value is YES.

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

{

 

 

Return YES;

 

 

}

 

// Set the tableViewCell editing Style

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

{

Return UITableViewCellEditingStyleDelete; // The default value is Delete.

// Return UITableViewCellEditingStyleInsert; // insert

 

 

 

 

 

}

 

// Complete editing

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

{

 

 

// Delete data,

// NSMutableArray * onegroup = _ dataArray [indexPath. section];

// [Onegroup removeObjectAtIndex: indexPath. row];

////

// NSArray * aeeay = [NSArray arrayWithObjects: indexPath, nil];

// NSArray * aeeay = @ [indexPath]; // same as the following

//

// Delete (n rows can be deleted)

If (editingStyle = UITableViewCellEditingStyleDelete ){

 

 

NSMutableArray * onegroup = _ dataArray [indexPath. section];

// If there is only one row left in the partition, delete the entire partition.

If (onegroup. count = 1 ){

[_ DataArray removeObject: onegroup]; // deletes data from a partition.

 

 

// Tableview Delete section

NSIndexSet * set = [NSIndexSet indexSetWithIndex: indexPath. section];

[_ TableView deleteSections: set withRowAnimation :( UITableViewRowAnimationLeft)];

 

 

} Else {

// Delete a row of data

[Onegroup removeObjectAtIndex: indexPath. row];

// NSArray * arr = [NSArray arrayWithObjects: indexPath, nil];

[TableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation :( UITableViewRowAnimationAutomatic)];

}

 

// [TableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation :( UITableViewRowAnimationAutomatic)]; // crash. Only rows are deleted. No data is deleted.

//// Three pieces of data are displayed on two rows (data must be deleted first)

 

}

 

 

// Add

If (editingStyle = UITableViewCellEditingStyleInsert ){

NSMutableArray * onegroup = _ dataArray [indexPath. section];

If (onegroup. count! = 0 ){

[TableView insertRowsAtIndexPaths: @ [indexPath] withRowAnimation :( UITableViewRowAnimationBottom)];

// _ Arr1 = [[NSMutableArray alloc] initWithObjects: @ "Beijing", nil];

[Onegroup addObject: @ "Beijing"];

_ DataArray = [[NSMutableArray alloc] initWithObjects: onegroup, nil];

[_ TableView reloadData];

NSLog (@ "cannot be added ");

 

}

 

}

 

 

 

 

 

}

 

 

// 2 set whether tableview can be moved

 

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

{

 

 

Return YES;

 

 

 

 

 

}

 

// 2. Complete the movement (data is processed by yourself)

 

-(Void) tableView :( UITableView *) tableView moveRowAtIndexPath :( NSIndexPath *) sourceIndexPath toIndexPath :( NSIndexPath *) destinationIndexPath

{

// Process data

// Find and obtain the corresponding group

NSMutableArray * onegroup = _ dataArray [sourceIndexPath. section];

// First retain the crude element of the medicated diet (to prevent problems during deletion)

NSString * name = [onegroup [sourceIndexPath. row] retain]; // find the element jia 1

// Delete an element

[Onegroup removeObject: name];

// Insert to the specified position

[Onegroup insertObject: name atIndex: destinationIndexPath. row];

[Name release];

 

 

}

 

// Set whether the cell can be moved to the scheduled destination propose (proxy)

 

-(NSIndexPath *) tableView :( UITableView *) tableView targetIndexPathForMoveFromRowAtIndexPath :( NSIndexPath *) sourceIndexPath toProposedIndexPath :( NSIndexPath *) proposedDestinationIndexPath

{

// If the range to be moved is in the same section, it can be moved.

If (sourceIndexPath. section = proposedDestinationIndexPath. section ){

Return proposedDestinationIndexPath; // return the destination

 

 

}

 

Return sourceIndexPath; // do not move across regions

// Return proposedDestinationIndexPath; // you can move

 

 

 

 

 

 

}

-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView

{

 

 

// Return 3;

Return [_ dataArray count];

 

 

}

 

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

{

// The three shards are 10, 15, and 20 rows respectively.

// If (section = 0 ){

// Return 10;

//}

// If (section = 1 ){

// Return 15;

//}

 

// Return 10;

NSArray * array = _ dataArray [section];

Return [array count];

 

 

}

 

 

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath

{

// Static NSString * identifier = @ "cell ";

// 1 retrieve the cell object from the queue

Static NSString * identifier = @ "cell ";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: identifier];

 

// 2 re-create if no

 

If (cell = nil ){

Cell = [[[UITableViewCell alloc] initWithStyle :( UITableViewCellStyleDefault) reuseIdentifier: identifier] autorelease];

 

}

/// 3 set the cell according to section and row to display the corresponding content.

// Cell. textLabel. text = [NSString stringWithFormat: @ "partition: % ld row: % ld", indexPath. section, indexPath. row];

//// (In this case, the cell may be created by a new one, which may be obtained from the queue)

/// 4 return the set cell object to tabelView

NSArray * array = _ dataArray [indexPath. section];

Cell. textLabel. text = array [indexPath. row];

 

Return cell;

 

 

}

 

 

//// Set the header title for each partition

//

//-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section

//{

//// Display 1 2 3

// Return [NSString stringWithFormat: @ "% ld", section + 1]; // The index starts from 0.

//

//

//}

 

// All

//-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView

//{

//

// Return @ [@ "1", @ "2", @ "3"];

//

//}

 

 

 

//// Set the Row Height

//-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath

//{

// If (indexPath. section = 0 ){

// Return 80;

//}

// If (indexPath. section = 1 ){

// Return 150;

//}

// Return 60;

//

//}

//

// Check whether the cell is selected

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

{

NSLog (@ "partition: % ld rows: % ld", indexPath. section, indexPath. row );

}

 

 

 

-(Void) didReceiveMemoryWarning

{

[Super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

/*

# Pragma mark-Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

-(Void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender

{

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

 

@ End


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.