UITableView of adding and deleting

Source: Internet
Author: User

APPDELEGATE.M file

#import "AppDelegate.h"

#import "RootTableViewController.h"

@interface Appdelegate ()

@end

@implementation Appdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Self.window.rootviewcontroller=[[uinavigationcontroller alloc]initwithrootviewcontroller:[[ Roottableviewcontroller Alloc]initwithstyle:uitableviewstyleplain]];

return YES;

}

ViewController.h file

#import <UIKit/UIKit.h>

1. Create an agreement

@protocol postviewdelegate <NSObject>

-(void) Postvalue: (NSString *) userName;

@end

@interface viewcontroller:uiviewcontroller<uitextfielddelegate>

@property (strong,nonatomic) NSString *name;

@property (strong,nonatomic) Uitextfield *textname;

@property (strong,nonatomic) ID <PostViewDelegate> Delegatepp;

@end

VIEWCONTROLLER.M file

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundcolor=[uicolor Greencolor];

Self.textname=[[uitextfield alloc]initwithframe:cgrectmake (100, 100, 200, 44)];

self.textname.delegate=self;

Self.textname.backgroundcolor=[uicolor Lightgraycolor];

Text Assignment

Self.textname.text=self.name;

Set Border Style

self.textname.borderstyle=1;

[Self.view AddSubview:self.textName];

}

-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField

{

if (Self.delegatepp) {

[Self.delegatepp PostValue:textField.text];

}

Keyboard control

if ([TextField Isfirstresponder]) {

[TextField Resignfirstresponder];

}

Out of the stack, showing the previous page

[Self.navigationcontroller Popviewcontrolleranimated:yes];

return YES;

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

RootTableViewController.h file

#import <UIKit/UIKit.h>

@interface Roottableviewcontroller:uitableviewcontroller

@property (strong,nonatomic) Nsmutablearray *students;

@end

ROOTTABLEVIEWCONTROLLER.M file

#import "RootTableViewController.h"

#import "ViewController.h"

@interface Roottableviewcontroller () <PostViewDelegate>

{

Member variables: Store index values for each click

Nsindexpath *currentindexpath;

}

@end

@implementation Roottableviewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Edit delete on the navigation bar

Self.navigationItem.rightBarButtonItem = Self.editbuttonitem;

Add Leftbarbuttonitem Increase

Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem:4 target:self action:@ Selector (AddItem)];

Data source

Self.students=[nsmutablearray arraywithcapacity:0];

[Self.students addobject:@ "Lisi"];

[Self.students addobject:@ "Zhangsan"];

[Self.students addobject:@ "Zhaoliu"];

[Self.students addobject:@ "Wangwu"];

Specify the reusable cell unique identity

[Self.tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "MyCell"];

}

Increase

-(void) AddItem

{

Uialertcontroller *alertcontroller=[uialertcontroller alertcontrollerwithtitle:@ "Add student Information" message:@ "Enter Name" Preferredstyle:uialertcontrollerstylealert];

Uialertaction *actionadd=[uialertaction actionwithtitle:@ "Add" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull action) {

Uitextfield *textname = alertcontroller.textfields[0];

[Self.students AddObject:textName.text];

NSLog (@ "True hyper-object");

[Self.tableview Reloaddata];

}];

[Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField) {

[Email protected] "Inpus name";

}];

[Alertcontroller Addaction:actionadd];

[Self Presentviewcontroller:alertcontroller animated:yes completion:^{

}];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

#pragma mark-table View data source

Returns the number of partitions

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {

return 1;

}

Returns the number of rows per partition displayed

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

{

return self.students.count;

}

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

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "MyCell" Forindexpath:indexpath];

What the main title displays

Cell.textlabel.text=self.students[indexpath.row]

;

return cell;

}

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath

{

Viewcontroller *view=[[viewcontroller Alloc]init];

Assign value

View.name=self.students[indexpath.row];

Specifying proxy objects

view.delegatepp=self;

Index value for each click

Currentindexpath=indexpath;

Next page

[Self.navigationcontroller Pushviewcontroller:view Animated:yes];

}

Implementation of Proxy Protocol method

-(void) Postvalue: (NSString *) userName

{

Self.students[currentindexpath.row]=username;

Refresh, Reload

[Self.tableview Reloaddata];

}

Override to support conditional editing of the table view.

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

Return NO If you don't want the specified item to be editable.

return YES;

}

Delete

Override to support editing the table view.

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

if (Editingstyle = = Uitableviewcelleditingstyledelete) {

Delete the row from the data source

[Self.students Removeobject:self.students[indexpath.row]];

Reload, equivalent to [Self.tableview Reloaddata], just delete which row, refresh which line, Withrowanimation:uitableviewrowanimationfade is add an animation effect

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

}

else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {

Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

}

}

Override to support rearranging the table view.

-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Fromindexpath Toindexpath: (Nsindexpath *) Toindexpath {

Finds the collection element at the specified location

NSString *name=self.students[fromindexpath.row];

Delete this collection element

[Self.students Removeobject:name];

Inserts this collection element to the specified position

[Self.students Insertobject:name AtIndex:toIndexPath.row];

NSLog (@ "%@", self.students);

}

Override to support conditional rearranging of the table view.

-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) Indexpath {

Return NO If you don't want the item to be re-orderable.

return YES;

}

@end

UITableView of adding and deleting

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.