增删改查

//
// 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值