Add, delete, and modify simple cells

Source: Internet
Author: User

Add, delete, and modify simple cells

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#import <UIKit/UIKit.h>#import "RootTableViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end

 

 

#import "AppDelegate.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;}

 

 

#import <UIKit/UIKit.h>@interface RootTableViewController : UITableViewController@property(strong,nonatomic)  NSMutableArray * array;@end

 

 

# Import "RootTableViewController. h "# import" ViewController. h "@ interface RootTableViewController () <postValuedelegate> {// record the index value of the selected row NSIndexPath * currentInfrxPath;} @ end @ implementation RootTableViewController-(void) viewDidLoad {[super viewDidLoad]; // Add liftbarabutton self. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @ Selector (addItem)]; self. navigationItem. rightBarButtonItem = self. editButtonItem; self. array = [NSMutableArray array]; [self. array addObject: @ "zhangsan"]; [self. array addObject: @ "lisi"]; [self. array addObject: @ "wangwu"]; [self. tableView registerClass: [UITableViewCell class] forCellReuseIdentifier: @ "reuseIdentifier"]; // Uncomment the following line to preserve selection between presentations. // s Elf. clearsSelectionOnViewWillAppear = NO; // Uncomment the following line to display an Edit button in the navigation bar for this view controller .} -(void) addItem {UIAlertController * alertcontroller = [UIAlertController alertControllerWithTitle: @ "are you sure you want to add" message: @ "input name? "PreferredStyle :( partial)]; UIAlertAction * OK = [UIAlertAction actionWithTitle: @" added "style: Adjust handler: ^ (UIAlertAction * _ Nonnull action) {UITextField * textName = alertcontroller. textFields [0]; [self. array addObject: textName. text]; [self. tableView reloadData]; // NSLog (@ "Real Operation") ;}]; [alertcontroller addTextFieldWithConfigurationHandler: ^ (UITextField * _ Nonnull textField) {textField. placeholder = @ "Add name";}]; [alertcontroller addAction: OK]; [self presentViewController: alertcontroller animated: YES completion: nil];}-(void) postvalue :( NSString *) username {// assign self to the element at the specified index position of the set. array [currentInfrxPath. row] = username; NSLog (@ "% @", username); // refresh data [self. tableView reloadData];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-Table view data source-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 1;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. array. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView metadata: @ "reuseIdentifier" forIndexPath: indexPath]; cell. textLabel. text = self. array [indexPath. row]; return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {currentInfrxPath = indexPath; ViewController * vc = [ViewController alloc] init]; vc. name = self. array [indexPath. row]; vc. delegate = self; [self. navigationController pushViewController: vc animated: YES];} // 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;} // 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. array removeObjectAtIndex: currentInfrxPath. row]; [tableView deleteRowsAtIndexPaths: @ [indexPath] withRowAnimation: UITableViewRowAnimationFade];} else if (editingStyle = inline) {// 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 {// 1. locate the NSString * name = self. array [fromIndexPath. row]; // 2. delete the set element [self. array removeObject: name]; // 3 insert a collection [self. array insertObject: name atIndex: toIndexPath. row]; NSLog (@ "% @", self. array);} // Override to support conditional rearranging of the table view. -(BOOL) tableView :( UITableView *) tableView canMoveRowAtIndexPath :( NSIndexPath *) indexPath {// Return NO if you do not want the item to be re-orderable. return YES;}/* # 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

 

 

#import <UIKit/UIKit.h>@protocol postValuedelegate <NSObject>-(void)postvalue:(NSString *) username;@end@interface ViewController : UIViewController<UITextFieldDelegate>@property(strong,nonatomic) NSString *name;@property(strong,nonatomic) UITextField * textNmae;@property(strong,nonatomic) id<postValuedelegate> delegate;@end

 

 

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor=[UIColor greenColor];    self.textNmae=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 44)];        self.textNmae.delegate=self;        self.textNmae.borderStyle=1;        self.textNmae.text=self.name;        [self.view addSubview:self.textNmae];}-(BOOL)textFieldShouldReturn:(UITextField *)textField{        if (self.delegate) {        [self.delegate postvalue:textField.text];    }    if ([textField isFirstResponder]) {        [textField resignFirstResponder];    }        [self.navigationController popViewControllerAnimated:YES];        return YES;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@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.