TableView的添加刪除

步驟

  • 模型數(shù)據(jù)從NSArray->NSMutableArray
  • 在故事板(Storyboard)中添加 增加按鈕 和 刪除按鈕

添加按鈕

- (IBAction)add {
    // tableView里面需要顯示新的cell數(shù)據(jù)赦肃,只需要操作模型數(shù)據(jù)
    XMGDeal *deal = [[XMGDeal alloc] init];
    deal.title = [NSString stringWithFormat:@"XX飯店大打折 %d折", arc4random_uniform(50)];
    deal.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
    deal.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
    deal.icon = @"5ee372ff039073317a49af5442748071";
    [self.deals insertObject:deal atIndex:0];
    
    XMGDeal *deal2 = [[XMGDeal alloc] init];
    deal2.title = [NSString stringWithFormat:@"YY飯店大打折 %d折", arc4random_uniform(50)];
    deal2.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
    deal2.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
    deal2.icon = @"5ee372ff039073317a49af5442748071";
    [self.deals insertObject:deal2 atIndex:0];
    
    // 提醒tabelView,模型數(shù)據(jù)發(fā)生了變化,請重新識別床估,請重新向數(shù)據(jù)源索要數(shù)據(jù)
    [self.tableView reloadData];
    // 插入某些特定的行
//    [self.tableView insertRowsAtIndexPaths:@[
//                                             [NSIndexPath indexPathForRow:0 inSection:0],
//                                             [NSIndexPath indexPathForRow:1 inSection:0]
//                                             ] withRowAnimation:UITableViewRowAnimationLeft];
}

刪除按鈕

- (IBAction)remove {
    // 移除模型數(shù)據(jù)
    [self.deals removeObjectAtIndex:0];
    [self.deals removeObjectAtIndex:0];
    [self.deals removeObjectAtIndex:0];
    
    // 刷新表格
    [self.tableView reloadData];
//    [self.tableView deleteRowsAtIndexPaths:@[
//                                             [NSIndexPath indexPathForRow:0 inSection:0],
//                                             [NSIndexPath indexPathForRow:1 inSection:0],
//                                             [NSIndexPath indexPathForRow:2 inSection:0]
//                                             ] withRowAnimation:UITableViewRowAnimationRight];
    
    // 15個cell:13
    // 15個模型:12
}

更新數(shù)據(jù)

- (IBAction)update {
//    XMGDealCell *cell = (XMGDealCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
//    cell.priceLabel.text = @"¥999";
//    
//    return;
    // 修改模型
    XMGDeal *deal = self.deals[3];
    deal.price = [NSString stringWithFormat:@"%d", 50 + arc4random_uniform(100)];;
    
    // 刷新表格
    [self.tableView reloadData];
//    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
}

進(jìn)入編輯模式

- (IBAction)switchEditing {
    // 進(jìn)入編輯模式
    //    self.tableView.editing = YES;
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}

左劃刪除-編輯模式

#pragma mark - TableView代理方法
/**
 * 只要實現(xiàn)這個方法尿孔,左劃cell出現(xiàn)刪除按鈕的功能就有了
 * 用戶提交了添加(點(diǎn)擊了添加按鈕)\刪除(點(diǎn)擊了刪除按鈕)操作時會調(diào)用
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {  // 點(diǎn)擊了“刪除”
        // 刪除模型
        [self.deals removeObjectAtIndex:indexPath.row];
        
        // 刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 點(diǎn)擊了+
        NSLog(@"+++++ %zd", indexPath.row);
    }
}
/**
 * 這個方法決定了編輯模式時鲁猩,每一行的編輯類型:insert(+按鈕)适滓、delete(-按鈕)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}

添加按鈕彈出框

  • UIAlertView 彈出框方法
  • IOS8之后方法:UIAlertController
- (IBAction)add {
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"111" message:@"2222" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
//    
//    [alertView show];
//    UIActionSheet *sheet;
    
    // 創(chuàng)建彈框控制器
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請輸入團(tuán)購信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按鈕
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // 創(chuàng)建模型
        XMGDeal *deal = [[XMGDeal alloc] init];
        deal.title = [alert.textFields[0] text];
        deal.price = [alert.textFields[1] text];
        [self.deals insertObject:deal atIndex:0];
        
        // 刷新數(shù)據(jù)
        [self.tableView reloadData];
    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"不知道" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"點(diǎn)擊了不知道按鈕");
//    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"不知道2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"點(diǎn)擊了不知道2按鈕");
//    }]];
    
    // 添加文本輸入框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"請輸入團(tuán)購名字";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"請輸入團(tuán)購價格";
    }];
    
    // 顯示控制器
    [self presentViewController:alert animated:YES completion:nil];
}

批量操作

  • 步驟
  • 給check選中按鈕添加到模型數(shù)據(jù)中
    @property (assign, nonatomic, getter=isCheck) BOOL check;
  • 連接輸出接口澳窑,并設(shè)置默認(rèn)隱藏
    @property (weak, nonatomic) IBOutlet UIImageView *check;
    self.check.hidden = !tg.isCheck;
  • 調(diào)用代理方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// 按鈕方法
- (IBAction)remove {
    // 臨時數(shù)組:存放即將需要刪除的團(tuán)購數(shù)據(jù)
    NSMutableArray *deletedTgs = [NSMutableArray array];
    for (YFtg *tg in self.tgs) {
        if (tg.isCheck) [deletedTgs addObject:tg];
    }
    // 刪除模型數(shù)據(jù)
    [self.tgs removeObjectsInArray:deletedTgs];
    [self.tableView reloadData];
}

// 代理方法:選中被刪除方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 點(diǎn)擊不被選中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    // 默認(rèn)隱藏皂甘,所以取反 打鉤
    YFtg *tg = self.tgs[indexPath.row];
    tg.check = !tg.isCheck;
    // 更新數(shù)據(jù)
    [self.tableView reloadData];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末玻驻,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子偿枕,更是在濱河造成了極大的恐慌璧瞬,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渐夸,死亡現(xiàn)場離奇詭異嗤锉,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)墓塌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門瘟忱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人苫幢,你說我怎么就攤上這事访诱。” “怎么了韩肝?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵触菜,是天一觀的道長。 經(jīng)常有香客問我哀峻,道長涡相,這世上最難降的妖魔是什么哲泊? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮催蝗,結(jié)果婚禮上切威,老公的妹妹穿的比我還像新娘。我一直安慰自己丙号,他們只是感情好先朦,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著槽袄,像睡著了一般烙无。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上遍尺,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天截酷,我揣著相機(jī)與錄音,去河邊找鬼乾戏。 笑死迂苛,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鼓择。 我是一名探鬼主播三幻,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼呐能!你這毒婦竟也來了念搬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤摆出,失蹤者是張志新(化名)和其女友劉穎朗徊,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體偎漫,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡爷恳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了象踊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片温亲。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖杯矩,靈堂內(nèi)的尸體忽然破棺而出栈虚,到底是詐尸還是另有隱情,我是刑警寧澤菊碟,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布节芥,位于F島的核電站,受9級特大地震影響逆害,放射性物質(zhì)發(fā)生泄漏头镊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一魄幕、第九天 我趴在偏房一處隱蔽的房頂上張望相艇。 院中可真熱鬧,春花似錦纯陨、人聲如沸坛芽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽咙轩。三九已至,卻和暖如春阴颖,著一層夾襖步出監(jiān)牢的瞬間活喊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工量愧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留钾菊,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓偎肃,卻偏偏與公主長得像煞烫,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子累颂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫滞详、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,121評論 4 61
  • 煙雨成雪冰凝水紊馏,白霧飛散忽現(xiàn)誰料饥。青衣碧發(fā)蘭溪上,紫衣夢醒卻成灰瘦棋。
    紫衣寒冰閱讀 220評論 0 0
  • 愛總是想像比現(xiàn)實美麗赌朋,相逢如是凰狞,告別亦如是∨媛——題記 他是愛抽煙的赡若,認(rèn)識他時,就是這樣短短的發(fā)下一雙大大的眼睛团甲,很...
    紅塵紫陌閱讀 286評論 0 5
  • 在當(dāng)今的時代,或者身腻,更直接的說在我的身邊产还。我看見過許多受他人影響迷失自我的人。經(jīng)常跟隨著他人的步伐嘀趟,甚至更...
    有點(diǎn)逗的小羊閱讀 470評論 3 4
  • 我怎么又在上課脐区,老師上完了課之后就順便布置了作業(yè)。不過我走神了她按,所以根本不知道老師說了啥牛隅。 不過還好老師又說了一次...
    愛夢的我閱讀 104評論 0 0