Oc AlertView 添加/删除/修改 到表格 -demo

注意:AlertView 要使用8.0版本
先添加导航条
控制器1

#import "ViewController.h"

@interface ViewController ()<UIActionSheetDelegate,UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic ,strong)UITableView *table;

@property(nonatomic ,strong)NSMutableArray *Marr;

@property(nonatomic ,assign)NSInteger integer;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title=@"表格";


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

    table.delegate=self;

    table.dataSource=self;

    [self.view addSubview:table];

    self.Marr=[NSMutableArray arrayWithObjects:@"n",@"n",@"n",@"n",@"n", nil];



    UIBarButtonItem * left =[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(Edit)];

    self.navigationItem.leftBarButtonItem=left;

    UIBarButtonItem * right =[[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(Add)];

    self.navigationItem.rightBarButtonItem=right;

    self.table = table;

}

//编辑方法

- (void)Edit {

    //开始编辑

    [self.table setEditing:!self.table.editing animated:YES];

}

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

    //从数据源上删除

    [self.Marr removeObjectAtIndex:0];

    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

    [self.table setEditing:NO animated:YES];

}

//添加和修改

-(void)Add{

    UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"信息" message:@"添加" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

    alert.alertViewStyle=UIAlertViewStylePlainTextInput;

    alert.delegate=self;

    alert.tag=100;

    [alert show];

}

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

    UITextField * text =[alertView textFieldAtIndex:0];

    if (buttonIndex==1&&alertView.tag==100) {

        [self.Marr addObject:text.text];

    }

    else if(buttonIndex==1 && alertView.tag==101){

        [self.Marr replaceObjectAtIndex:self.integer withObject:text.text];

    }

    [self.table reloadData];

}

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

    return self.Marr.count;

}

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

    static NSString * cellID =@"cell";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell==nil) {

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

    }

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

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.integer=indexPath.row;

    UIActionSheet * action =[[UIActionSheet alloc]initWithTitle:@"修改信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"修改", nil];

    action.delegate=self;

    [action showInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex==0) {

        UIAlertView * alert1 =[[UIAlertView alloc]initWithTitle:@"信息" message:@"修改信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

        alert1.alertViewStyle=UIAlertViewStylePlainTextInput;

        alert1.delegate=self;

        alert1.tag=101;

        [alert1 show];

    }

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值