表格的增删改查操作

单纯的表格增删改查 没有数据库

//  AppDelegate.m

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

//导航栏标题

theVc.title = @"姓名名单";

UINavigationController *theNav = [[UINavigationController alloc]initWithRootViewController:theVc];

//导航栏颜色

theNav.navigationBar.backgroundColor = [UIColor whiteColor];

self.window.rootViewController = theNav;


ViewController.m
//签协议

{

NSMutableArray *theArray;

NSInteger theIndex;

NSInteger intar;

UITextField *theTF;

}

//表格

-(UITableView *)theTableView

{

if (!_theTableView)

{

_theTableView = [[UITableView alloc] initWithFrame:self.view.frame];

//签协议

_theTableView.delegate = self;

_theTableView.dataSource = self;

//设置表格红线

_theTableView.separatorColor = [UIColor redColor];

}

return _theTableView;

}

=============== -(void)viewDidLoad ==========================================================

//添加到视图

[self.view addSubview:self.theTableView];

//数组初始化

theArray = [NSMutableArray arrayWithObjects:@"莫文蔚",@"奚梦瑶",@"陈乔恩",@"谢娜",@"刘嘉玲", nil];

//添加导航控制右按钮

UIBarButtonItem *theLeft = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(click)];

UIBarButtonItem *theRight = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];

//将导航控制器按钮添加到视图上

self.navigationItem.rightBarButtonItem = theRight;

self.navigationItem.leftBarButtonItem = theLeft;

////添加导航栏按钮另一种系统风格方法

//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleDone target:self action:@selector(click)];

//

////导航栏右按钮

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"+" style:UIBarButtonItemStyleDone target:self action:@selector(add)];

=========================================================================

//左按钮响应事件

-(void)click

{

//判断是否进行删除或不删除

[self.theTableView setEditing:!self.theTableView.editing animated:YES];

}

//右按钮响应事件

-(void)add

{

//判断和编辑不会同时存在

[self.theTableView setEditing:NO animated:YES];

//弹出框

UIAlertView *theAlertView = [[UIAlertView alloc]initWithTitle:nil message:@"please" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Ok", nil];

//文本框

theAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;

//判断标识符

theAlertView.tag = 1000;

//添加到视图

[theAlertView show];

//刷新表格

[self.theTableView reloadData];

}

//执行添加

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

UITextField *theTF1 = [alertView textFieldAtIndex:0];

if (buttonIndex == 1)

{

if (alertView.tag == 1000)

{

[theArray addObject:theTF1.text];

}

}

//刷新表格

[self.theTableView reloadData];

}

#pragma -

#pragma  mark - UITableViewDataSource

//表格要显示的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return theArray.count;

}

//侧滑删除

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

[theArray removeObjectAtIndex:indexPath.row];

//刷新表格

[self.theTableView reloadData];

}

//表格与单元格的关联

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat

{

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

if (!cell)

{

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

cell.textLabel.text = theArray[indexPat.row];

return cell;

}

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

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否进行修改" delegate:self cancelButtonTitle:@"放弃修改" otherButtonTitles:@"确认修改" , nil];

intar = indexPath.row;

alert.tag = 8888;

[alert show];

//刷新表格

[self.theTableView reloadData];

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;{

//执行修改内容输入框

if(alertView.tag == 8888)

{

if (buttonIndex == 1)

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入修改内容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定" , nil];

//弹出框风格

alert. alertViewStyle = UIAlertViewStylePlainTextInput;

alert.tag = 6666;

theTF = [alert textFieldAtIndex:0];

[alert show];

}

}

//替换修改

else if (alertView.tag == 6666)

{

if (buttonIndex == 1)

{

[theArray replaceObjectAtIndex:intar withObject:theTF.text];

}

[self.theTableView reloadData];

}

}

谢谢

你可能感兴趣的:(表格的增删改查操作)