添加删除(表格)

#import "ViewController.h"@interface ViewController ()@property (nonatomic,strong) NSMutableArray *array;

@property (nonatomic,strong) UITableView *tableViews;

@property (nonatomic,strong) UIAlertView *alert;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self.view setBackgroundColor:[UIColor whiteColor]];

[self.view addSubview:self.tableViews];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];

}

- (void)edit{

if (self.tableViews.isEditing == NO) {

self.tableViews.editing = YES;

}else if (self.tableViews.isEditing == YES){

self.tableViews.editing = NO;

}

}

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

if (buttonIndex == 1) {

UITextField *tf = [self.alert textFieldAtIndex:0];

[self.array addObject:tf.text];

[self.tableViews reloadData];

}

}

- (void)add{

self.alert = [[UIAlertView alloc]initWithTitle:@"提示框" message:@"输入您的姓名" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];

self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;

[self.alert show];

}

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

return self.array.count;

}

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

[self.array removeObject:self.array[indexPath.row]];

[self.tableViews reloadData];

}

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

static NSString *tableIdentfier = @"table";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentfier];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentfier];

}

cell.textLabel.text = self.array[indexPath.row];

return cell;

}

- (NSMutableArray *)array{

if (!_array) {

_array = [[NSMutableArray alloc]initWithObjects:@"张三",@"李四",@"王五", nil];

}

return _array;

}

- (UITableView *)tableViews{

if (!_tableViews) {

_tableViews = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

[_tableViews setDelegate:self];

[_tableViews setDataSource:self];

}

return _tableViews;

}

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,098评论 3 38
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 576评论 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 982评论 0 0
  • #import<Foundation/Foundation.h> #import #import "sqlTest...
    lichengjin阅读 433评论 0 0
  • 如题,我想做个超人!在今晚公司停电,电脑里写到一半的文章拿不出来的时候,能变身超人找出故障并恢复!而不会像现在一样...
    安好007阅读 205评论 1 1