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

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

#import "ViewController.h"

@interface ViewController ()

@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



你可能感兴趣的:(Oc AlertView 添加/删除/修改 到表格 -demo)