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

#import "ViewController.h"

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

@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 {

//開(kāi)始編輯

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

}

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

//從數(shù)據(jù)源上刪除

[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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類(lèi)似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • 哦吼吼,又研究了幾天,把FMDB這個(gè)封裝好的數(shù)據(jù)庫(kù)搞定了,寫(xiě)了個(gè)簡(jiǎn)單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 665評(píng)論 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個(gè)文章 AppDelegate.h ...
    CC_iOS閱讀 1,184評(píng)論 0 0
  • iOS開(kāi)發(fā)系列--網(wǎng)絡(luò)開(kāi)發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開(kāi)發(fā),例如說(shuō)新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,037評(píng)論 2 7
  • 半夜里,睡得正香,突然聽(tīng)到入戶(hù)門(mén)鎖響的聲音,雖然被驚醒,但大概也是半夜的緣故,有些迷糊,我看著門(mén)是關(guān)的,再想睡覺(jué)前...
    梅子Mey閱讀 332評(píng)論 5 1

友情鏈接更多精彩內(nèi)容