//
// TwoViewController.m
// week2
//
// Created by 森屿猫 on 2019/1/7.
// Copyright © 2019 徐磊. All rights reserved.
//
#import “TwoViewController.h”
@interface TwoViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>{
UITableView *theTable;
NSMutableArray *array;
}
@end
@implementation TwoViewController
-
(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @“省会信息”;
// 初始化表格
theTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// 代理
theTable.delegate = self;
theTable.dataSource = self;[self.view addSubview:theTable];
// 右按钮
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@“添加” style:UIBarButtonItemStylePlain target:self action:@selector(right)];
self.navigationItem.rightBarButtonItem = right;
// 左按钮
UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@“编辑” style:UIBarButtonItemStylePlain target:self action:@selector(left)];
self.navigationItem.leftBarButtonItem = left;array = [[NSMutableArray alloc] initWithObjects:@“北京”,@“上海”,@“南京”,@“杭州”,nil];
}
-(void)left
{
[theTable setEditing:!theTable.editing animated:YES];
}
// 添加
-(void)right
{
UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@“信息” message:@“添加” delegate:self cancelButtonTitle:@“取消” otherButtonTitles:@“确定”, nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
alert.delegate=self;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
UITextField *field = [alertView textFieldAtIndex:0];
[array addObject:field.text];
[theTable reloadData];
}
}
#pragma mark - 表格方法
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
} -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
}cell.textLabel.text = array[indexPath.row];
return cell;
} -
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{[array removeObjectAtIndex:indexPath.row];
[theTable reloadData];
}
@end