Table View 导航按钮

2017-08-13  本文已影响0人  Willism

效果:


AppDelegate.m

创建导航栏


ViewController.m

#import "ViewController.h"

@interface ViewController (){

UITableView *_tableView;

NSMutableArray *_arr;

UITextField *_text,*_text1;

NSInteger num;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 添加标题

self.title = @"信息管理";

// 初始化数组

_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

// 设置代理

_tableView.delegate = self;

_tableView.dataSource = self;

// 添加到视图上

[self.view addSubview:_tableView];

// 初始化数组并赋值

_arr = [NSMutableArray arrayWithObjects:@"张三",@"李四",@"王五", nil];

// 添加左右按钮

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];

}

#pragma -

#pragma mark - UITableViewDelegate

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

{

return _arr.count;

}

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

{

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

if (cell == nil)

{

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

}

cell.textLabel.text = _arr[indexPath.row];

return cell;

}

#pragma -

#pragma mark - 左右按钮事件

- (void)leftAction

{

if (_tableView.editing == NO)

{

[_tableView setEditing:YES animated:YES];

}

else

{

[_tableView setEditing:NO animated:YES];

}

}

- (void)rightAction

{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"添加数据" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

// 设置风格

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

_text = [alert textFieldAtIndex:0];

alert.tag = 1000;

[alert show];

}

#pragma -

#pragma mark - UIAlertViewDelegate

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

{

if (alertView.tag == 1000)

{

if (buttonIndex == 1)

{

[_arr addObject:_text.text];

}

[_tableView reloadData];

}

else if (alertView.tag == 1001)

{

if (buttonIndex == 1)

{

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

alert2.alertViewStyle = UIAlertViewStylePlainTextInput;

_text1 = [alert2 textFieldAtIndex:0];

alert2.tag = 1002;

[alert2 show];

}

}

else if (alertView.tag == 1002)

{

if (buttonIndex == 1)

{

[_arr replaceObjectAtIndex:num withObject:_text1.text];

}

[_tableView reloadData];

}

}

// 进入编辑状态的方法

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

{

[_arr removeObjectAtIndex:indexPath.row];

[_tableView reloadData];

}

// 点击任一行

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

{

UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否修改单元格内容" delegate:self cancelButtonTitle:@"放弃修改" otherButtonTitles:@"修改", nil];

alert1.tag = 1001;

[alert1 show];

num = indexPath.row;

}

上一篇 下一篇

猜你喜欢

热点阅读