ios -- UITableView簡(jiǎn)單的移動(dòng)刪除Demo

當(dāng)我們點(diǎn)擊添加按鈕時(shí)可以向提示框輸入數(shù)據(jù)添加到表格,當(dāng)我們點(diǎn)擊編輯時(shí)我們可以對(duì)表格進(jìn)行移動(dòng)刪除操作

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{

UITableView *_table;

NSMutableArray *marr;

UIButton *leftBtn;

}

@end

@implementation ViewController

//這里主要實(shí)現(xiàn)表格的初始化,數(shù)組的初始化,左右導(dǎo)航按鈕的初始化

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.title = @"表格";

//添加編輯按鈕

leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];

[leftBtn setTitle:@"編輯" forState:UIControlStateNormal];

[leftBtn setTitle:@"完成" forState:UIControlStateSelected];

leftBtn.frame = CGRectMake(0, 0, 80, 40);

leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];

//標(biāo)題顏色

[leftBtn setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];

[leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:leftBtn];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnClicked:)];

//創(chuàng)建表格并初始化

_table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

_table.delegate = self;

_table.dataSource = self;

//將表格添加到視圖

[self.view addSubview:_table];

//初始化三個(gè)人名

marr = [NSMutableArray arrayWithObjects:@"王二",@"劉山",@"李四", nil];

}

#pragma mark - 導(dǎo)航按鈕點(diǎn)擊觸發(fā)方法

- (void)leftBarBtnClicked:(id)sender {

//設(shè)置tableview編輯狀態(tài)

BOOL flag = !_table.editing;

[_table setEditing:flag animated:YES];

leftBtn.selected = flag;

}



- (void)rightBarBtnClicked:(id)sender {

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"請(qǐng)輸入"

message:@"請(qǐng)輸入姓名"

delegate:self cancelButtonTitle:@"確定"

otherButtonTitles:@"取消", nil];

[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];

[alertView show];

}



#pragma mark? 獲得輸入框里的值

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

NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];

if ([buttonTitle isEqualToString:@"確定"]){

UITextField *tf=[alertView textFieldAtIndex:0];//獲得輸入框

NSString * res = tf.text;//獲得值

NSLog(@"%@",res);

[marr addObject:res];

[_table reloadData];

}

}

#pragma mark - 表格方法

#pragma mark 選中行

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

{

// 取消選中狀態(tài)

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}


//表格有多少行

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

return marr.count;

}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];

}

//將人名添加到表格

cell.textLabel.text = marr[indexPath.row];

//? ? cell.accessoryType = UITableViewCellAccessoryNone;//cell沒(méi)有任何的樣式

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個(gè)小箭頭耳鸯,距離右邊有十幾像素撇寞;

//? ? cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個(gè)藍(lán)色的圓形button;

//? ? cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對(duì)號(hào)胡控;


return cell;

}

#pragma mark - 提交編輯操作

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

return YES;

}

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

{

//只要實(shí)現(xiàn)這個(gè)方法扎瓶,就實(shí)現(xiàn)了默認(rèn)滑動(dòng)刪除Q罕啤s锎搿!0!

if (editingStyle != UITableViewCellEditingStyleDelete)

return;

//刪除數(shù)據(jù)模型

[marr removeObjectAtIndex:indexPath.row];

[_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

#pragma mark - 移動(dòng)操作

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the item to be re-orderable.

return YES;

}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

//起始位置

NSInteger fromRow = fromIndexPath.row;

//終止位置

NSInteger toRow = toIndexPath.row;

NSLog(@"%ld,%ld",fromRow,toRow);

//先取出起始位置的數(shù)據(jù)

NSString *fromContent = marr[fromRow];

//把起始位置的數(shù)據(jù)插入終止位置

[marr insertObject:fromContent atIndex:toRow];

NSLog(@"%@",marr);

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末班套,一起剝皮案震驚了整個(gè)濱河市肢藐,隨后出現(xiàn)的幾起案子故河,更是在濱河造成了極大的恐慌吱韭,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鱼的,死亡現(xiàn)場(chǎng)離奇詭異理盆,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)凑阶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)猿规,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人宙橱,你說(shuō)我怎么就攤上這事姨俩。” “怎么了师郑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵环葵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我宝冕,道長(zhǎng)张遭,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任地梨,我火速辦了婚禮菊卷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘宝剖。我一直安慰自己洁闰,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布万细。 她就那樣靜靜地躺著渴庆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪雅镊。 梳的紋絲不亂的頭發(fā)上襟雷,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天,我揣著相機(jī)與錄音仁烹,去河邊找鬼耸弄。 笑死,一個(gè)胖子當(dāng)著我的面吹牛卓缰,可吹牛的內(nèi)容都是我干的计呈。 我是一名探鬼主播砰诵,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼捌显!你這毒婦竟也來(lái)了茁彭?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤扶歪,失蹤者是張志新(化名)和其女友劉穎理肺,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體善镰,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡妹萨,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了炫欺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乎完。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖品洛,靈堂內(nèi)的尸體忽然破棺而出树姨,到底是詐尸還是另有隱情,我是刑警寧澤桥状,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布帽揪,位于F島的核電站,受9級(jí)特大地震影響岛宦,放射性物質(zhì)發(fā)生泄漏台丛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一砾肺、第九天 我趴在偏房一處隱蔽的房頂上張望挽霉。 院中可真熱鬧,春花似錦变汪、人聲如沸侠坎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)实胸。三九已至,卻和暖如春番官,著一層夾襖步出監(jiān)牢的瞬間庐完,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工徘熔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留门躯,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓酷师,卻偏偏與公主長(zhǎng)得像讶凉,于是被迫代替她去往敵國(guó)和親染乌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容