添加删除(表格)

#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;

}

你可能感兴趣的:(添加删除(表格))