Table View 导航按钮

效果:


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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,097评论 3 38
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 982评论 0 0
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 576评论 0 0
  • 一直以为导致单身汪的原因是男人不敢主动,女人不敢进攻,今天看了调查单身原因的报表才大彻大悟,原来原因百分之八十都集...
    Play恋爱阅读 997评论 2 1
  • 来自一个不会画画,但是喜欢动笔的小姑娘,记录成长。 一直觉得看书,画画,是一个很享受的过程,一低头,一抬头,几个小...
    你的猫_阅读 304评论 6 9