IOS往表格上添加删除修改文字---by talent.L

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic ,strong)UITableView*qjtable;

@property(nonatomic ,strong)NSMutableArray *Marr;

@property(nonatomic ,assign)NSInteger integer;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

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

self.view.backgroundColor=[UIColor whiteColor];

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

[self.view addSubview:table];

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

table.delegate=self;

table.dataSource=self;

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

self.navigationItem.leftBarButtonItem=left;

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

self.navigationItem.rightBarButtonItem=right;

self.qjtable=table;

}

//编辑方法

- (void)did {

//开始编辑

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

}

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

//从数据源上删除

[self.Marr removeObjectAtIndex:0];

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

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

}

//添加和修改

-(void)did1{

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.qjtable 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

你可能感兴趣的:(IOS往表格上添加删除修改文字---by talent.L)