首页 > 其他 > 详细

UITableView的增删改插

时间:2016-03-18 23:21:31      阅读:336      评论:0      收藏:0      [点我收藏+]

//  AppDelegate.m文件

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

#import <UIKit/UIKit.h>

 

// 1、创建协议

@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

 

UITableView的增删改插

原文:http://www.cnblogs.com/Always-LuoHan/p/5293910.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!