UITableView的增,删,改例子
1.指定跟视图
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//设置跟视图为[RootTableViewController new]并开创一个导航栏
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[RootTableViewController new]];
return YES;
}
2.ViewController类.h文件
#import <UIKit/UIKit.h>
//1.创建协议
@protocol PostValueDelegatee <NSObject>
//声明一个协议方法用于传值。
-(void)postValue:(NSString *) username;
@end
//遵循<UITextFieldDelegate>协议
@interface ViewController : UIViewController<UITextFieldDelegate>
//定义一个NSString的属性来传值
@property (nonatomic ,strong) NSString *name;
//定义UITextField *txtName来修改上页的值
@property (nonatomic ,strong) UITextField *txtName;
//代理类型的属性
@property (nonatomic ,assign) id<PostValueDelegatee> delegate;
@end
3.ViewController类.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.txtName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
//指定代理
self.txtName.delegate = self;
//设置边框样式
self.txtName.borderStyle = 1;
//文本赋值
self.txtName.text = self.name;
[self.view addSubview:self.txtName];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//调用代理方法
if (self.delegate) {
[self.delegate postValue:textField.text];
}
//失去第一相应值,即键盘消失
if([textField isFirstResponder]){
[textField resignFirstResponder];
}
// 出栈 显示上一页
[self.navigationController popToRootViewControllerAnimated:YES];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.创建一个RootTableViewController类 继承 UITableViewController
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface RootTableViewController : UITableViewController
//定义一个可变 集合 NSMutableArray *students来存学生的信息
@property (nonatomic ,strong) NSMutableArray *students;
@end
5.RootTableViewController.m文件
#import "RootTableViewController.h"
@interface RootTableViewController ()<PostValueDelegatee>
{
NSIndexPath *currentIndexPath;
}
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
//添加leftBarButtonItem
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)];
//添加一个向右的.navigationItem.rightBarButtonItem编辑按钮
self.navigationItem.rightBarButtonItem =self.editButtonItem;
//初始化可变集合
self.students = [NSMutableArray array];
//给可变集合添加几个元素
[self.students addObject:@"lisi"];
[self.students addObject:@"zhangsan"];
[self.students addObject:@"wangwu"];
//指定单元格唯一标识reuseIdentifier
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
}
//定义增加信息的方法
-(void)addItem{
//初始化UIAlertController *alert 对象来提示增加学生信息
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"增加学生信息" message:@"输入姓名" preferredStyle:UIAlertControllerStyleAlert];
//定义UIAlertAction *actionAdd增加按钮,点击增加按钮 确定增加
UIAlertAction *actionAdd = [UIAlertAction actionWithTitle:@"增加" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//定义一个UITextField *field来接收输入的信息
UITextField *field = alert.textFields[0];
//把信息添加到可变集合self.students里面
[self.students addObject:field.text];
//刷新数据
[self.tableView reloadData];
}];
//提示textField信息
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//提示信息@"input name";
textField.placeholder = @"input name";
}];
//把增加信息的actionAdd添加到alert
[alert addAction:actionAdd];
//显示弹框
[self presentViewController:alert animated:YES completion:nil];
}
//程序将要加载之前刷新数据
-(void)viewDidAppear:(BOOL)animated{
[super viewWillAppear:YES];
//刷新数据
[self.tableView reloadData];
}
//实现协议的方法
-(void)postValue:(NSString *)username{
//为集合指定缩影位置元素赋值
self.students[currentIndexPath.row] = username;
}
- (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:@"reuseIdentifier" forIndexPath:indexPath];
// 把学生的信息添加到tableview上
cell.textLabel.text =self.students[indexPath.row];
// Configure the cell...
//返回信息
return cell;
}
//显示选中单元格的内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//定义的一个全局变量来接收indexPath
currentIndexPath = indexPath;
//实例化一个ViewController *vc的对象
ViewController *vc = [[ViewController alloc] init];
//通过name属性把信息传到下一页
vc.name = self.students[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.students removeObjectAtIndex:indexPath.row];
[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];
//2.删除集合元素
[self.students removeObject:name];
//3.插入集合元素到指定位置
[self.students insertObject:name atIndex:toIndexPath.row];
NSLog(@"%@",self.students);
}