Ui Study Notes --- uitableview table view editing on the tenth day

Source: Internet
Author: User

Edit uitableview table View

Use Cases of table view editing

When you need to manually add or delete a piece of data to tableview, you can use tableview to edit it. For example, you can delete a call with someone in the buckle.

When we need to manually adjust the sequence of cells, we can move the cells to the specified position through tableview.

Code in proxy appdelegate. m

#import "AppDelegate.h"#import "RootViewController.h"@implementation AppDelegate-(void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];        RootViewController *rootVC = [[RootViewController alloc] init];    UINavigationController *ngVC = [[UINavigationController alloc] initWithRootViewController:rootVC];    self.window.rootViewController = ngVC;        [ngVC release];    [rootVC release];    [self.window makeKeyAndVisible];    return YES;}

Create a variable array attribute in rootviewcontroller. h.

Nsmutablearray * _ Marr;

Code for initialization and loadview in rootviewcontroller. m

-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) attributes {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {self. navigationitem. title = @ "Lecture Hall"; self. navigationitem. rightbarbuttonitem = self. editbuttonitem; // The edit button provided by the Controller _ Marr = [[nsmutablearray alloc] initwithobjects: @ "Zhao", @ "", @ "sun", @ "Li ", @ "Week", @ "Wu", @ "zheng", @ "Wang", @ "Tang Miao", @ "Sun Wukong", @ "", @ "Sha Seng ", @ "Tom", @ "Erlang", @ "", @ "Lei zhenzi", nil]; // custom initialization} return self;}-(void) loadview {uitableview * Table = [[uitableview alloc] initwithframe: cgrectmake (0, 0,320,480) style: uitableviewstylegrouped]; table. datasource = self; table. delegate = self; self. view = table; [Table release];}

 

 

-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {return [_ Marr count];} (uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * identifier = @ "reuse"; uitableviewcell * cell = [tableview metadata: identifier]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: identifier];} cell. textlabel. TEXT = [_ Marr objectatindex: indexpath. row]; // tableview. editing = yes; // cell attribute on the right of the cell. accessorytype = uitableviewcellaccessorydisclosureindicator; return cell ;}

 

Edit procedure

1. Make tableview editable

-(Void) setediting :( bool) Editing animated :( bool) animated {// call the parent class method to implement the transformation between edit and done [Super setediting: editing animated: animated]; uitableview * tableview = (uitableview *) self. view; [tableview setediting: editing animated: animated];}

2. Specify the rows in tableview that can be edited.

// Set the Editable status of the cell. The default value is yes-(bool) tableview :( uitableview *) tableview caneditrowatindexpath :( nsindexpath *) indexpath {// If (indexpath. row = 0) {// return yes; //} return yes ;}

3. Specify the style for editing tableview (Add. delete)

// Method in delegate-(uitableviewcelleditingstyle) tableview :( uitableview *) tableview editingstyleforrowatindexpath :( nsindexpath *) indexpath {If (indexpath. row = 0) {return uitableviewcelleditingstyleinsert; // Add} return uitableviewcelleditingstyledelete; // Delete}

4. Edit (operate the data source first and then modify the UI)

// Event triggered when you click the plus sign or delete-(void) tableview :( uitableview *) tableview commiteditingstyle :( uitableviewcelleditingstyle) editingstyle forrowatindexpath :( nsindexpath *) indexpath {If (editingstyle = deleted) {[tableview beginupdates]; // delete data (write it before the cell deletion or write a [tableview beginupdates] and put it behind the [tableview endupdates]) [_ Marr removeobjectatindex: indexpath. row]; // Delete cell [tableview deleterowsatindexpaths: @ [indexpath] withrowanimation: uitableviewrowanimationright]; [tableview endupdates]; nslog (@ "delete ");} else {[tableview beginupdates]; [_ Marr insertobject: @ "hello" atindex: indexpath. row]; [tableview insertrowsatindexpaths: @ [indexpath] withrowanimation: uitableviewrowanimationright]; [tableview endupdates]; nslog (@ "add ");}}

Moving table views

Steps for moving

1. Make tableview editable

-(Void) setediting :( bool) Editing animated :( bool) animated {// call the parent class method to implement the transformation between edit and done [Super setediting: editing animated: animated]; uitableview * tableview = (uitableview *) self. view; [tableview setediting: editing animated: animated];}

2. Specify which rows of tableview can be moved

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}

3. Move completed

-(Void) tableview :( uitableview *) tableview moverowatindexpath :( nsindexpath *) sourceindexpath toindexpath :( nsindexpath *) destinationindexpath {nsstring * STR = [_ Marr objectatindex: sourceindexpath. row]; // Add 1 to the reference count. avoid the occurrence of the wild pointer [STR retain]; // Delete the element [_ Marr removeobjectatindex: sourceindexpath. row]; // Insert the element [_ Marr insertobject: Str atindex: destinationindexpath. row]; [STR release]; // The retain object before release}

Monitors the movement process to restrict cross-zone Movement

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    NSLog(@"%d",sourceIndexPath.row);    NSLog(@"%d",proposedDestinationIndexPath.row);    if (sourceIndexPath.row == [_mArr count]-1) {        return sourceIndexPath;    }else{    return proposedDestinationIndexPath;    }}

Uitableviewcontroller table View Controller

Inherited from uiviewcontroller

Comes with a tableview. The root view is tableview.

The template comes with mobile-related code editing

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.