UITableView的增刪改插

//  AppDelegate.m文件app

#import "AppDelegate.h"ide

#import "RootTableViewController.h"post

@interface AppDelegate ()動畫

 

@endatom

 

@implementation AppDelegate代理

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {對象

   

    

    self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[RootTableViewController alloc]initWithStyle:UITableViewStylePlain]];索引

    

    return YES;ci

}rem

 

//  ViewController.h文件

#import <UIKit/UIKit.h>

 

// 一、建立協議

@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文件

 

#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];

    

    //文本賦值

    self.textName.text=self.name;

    

    //設置邊框樣式

    self.textName.borderStyle=1;

    [self.view addSubview:self.textName];   

}

 

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

    

    if (self.delegatepp) {

        [self.delegatepp 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

 

 

//  RootTableViewController.h文件

 

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController

@property (strong,nonatomic) NSMutableArray *students;

@end

 

//  RootTableViewController.m文件

 

#import "RootTableViewController.h"

#import "ViewController.h"

@interface RootTableViewController ()<PostViewDelegate>

{

    //成員變量:存儲每次點擊的索引值

    NSIndexPath *currentIndexPath;

}

 

@end

 

@implementation RootTableViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

    //導航欄上的Edit  刪除

     self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

    //添加leftBarButtonItem   增長

    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:4 target:self action:@selector(addItem)];

    

    //數據源

    self.students=[NSMutableArray arrayWithCapacity:0];

    [self.students addObject:@"lisi"];

    [self.students addObject:@"zhangsan"];

    [self.students addObject:@"zhaoliu"];

    [self.students addObject:@"wangwu"];

    

    //指定重用單元格惟一標識

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myCell"];

 

}

 

//增長

-(void)addItem

{

    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"增長學生信息" message:@"輸入姓名" preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *actionAdd=[UIAlertAction actionWithTitle:@"增長" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        

      UITextField *textName = alertController.textFields[0];

        

        

        [self.students addObject:textName.text];

        //NSLog(@"真正的超做對象");

        [self.tableView reloadData];

    }];

    

    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        textField.placeholder=@"inpus name";

    }];

    

    [alertController addAction:actionAdd];

    

    [self presentViewController:alertController animated:YES completion:^{

        

    }];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // 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.students.count;

}

 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];

    //主標題顯示的內容

    cell.textLabel.text=self.students[indexPath.row]

    ;

    return cell;

}

 

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

{

    ViewController *view=[[ViewController alloc]init];

    

    //賦值

    view.name=self.students[indexPath.row];

    //指定代理對象

    view.delegatepp=self;

    //每次點擊的索引值

    currentIndexPath=indexPath;

    //下一頁

    [self.navigationController pushViewController:view animated:YES];

    

}

 

//代理協議方法的實現

-(void)postValue:(NSString *)userName

{

    self.students[currentIndexPath.row]=userName;

    //刷新,從新加載

    [self.tableView reloadData];

     }

 

// 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.students removeObject:self.students[indexPath.row]];

        

        //從新加載,至關於[self.tableView reloadData],只是刪除哪行,刷新哪行,  withRowAnimation:UITableViewRowAnimationFade 是加一個動畫效果

        [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 {

    //找到指定位置的集合元素

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

    //刪除此集合元素

    [self.students removeObject:name];

    

    //插入此集合元素到指定位置

    [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 do not want the item to be re-orderable.

    return YES;

}

 

 

@end

相關文章
相關標籤/搜索