UITableView 編輯模式

UITableView 編輯模式

相關(guān)方法

UITableView

插入赏枚、刪除和移動(dòng)表格的行和分區(qū) (Inserting, Deleting, and Moving Rows and Sections)
//對(duì)表格執(zhí)行多個(gè)連續(xù)的插入筒饰、刪除和移動(dòng)操作前,調(diào)用開(kāi)始更新
- (void)beginUpdates; 
//對(duì)表格執(zhí)行多個(gè)連續(xù)的插入、刪除和移動(dòng)操作后,調(diào)用結(jié)束更新
- (void)endUpdates; 

//在一個(gè)或多個(gè)indexPath處插入cell
- insertRowsAtIndexPaths: withRowAnimation:
//刪除一個(gè)或多個(gè)indexPath處的cell
- deleteRowsAtIndexPaths: withRowAnimation:

//將指定indexPath對(duì)應(yīng)的cell移動(dòng)到另一個(gè)indexPath
- moveRowAtIndexPath: toIndexPath:
//在表格中用所選的動(dòng)畫插入一個(gè)或多個(gè)分區(qū)
- insertSections: withRowAnimation:
//在表格中用所選的動(dòng)畫刪除一個(gè)或多個(gè)分區(qū)
- deleteSections: withRowAnimation: 
//將指定分區(qū)移動(dòng)到另一個(gè)位置
- moveSection: toSection: 
Managing the Editing of Table Cells
//默認(rèn)值NO. YES則進(jìn)入編輯狀態(tài) 
BOOL editing 
- setEditing: animated: 

UITableViewDataSource

插入或刪除表格行 (Inserting or Deleting Table Rows)
//對(duì)指定cell編輯完成時(shí)觸發(fā)
- tableView: commitEditingStyle: forRowAtIndexPath:
 
//返回值決定指定indexPath對(duì)應(yīng)的cell是否可編輯  
- (BOOL)tableView: canEditRowAtIndexPath: 
重新排列表格行 (Reordering Table Rows)
//返回值決定指定indexPath對(duì)應(yīng)的cell是否可移動(dòng)
- (BOOL)tableView: canMoveRowAtIndexPath: 

//該方法告訴DataSource將指定的cell移動(dòng)到另一個(gè)位置
//單元格的移動(dòng)相關(guān)
- tableView: moveRowAtIndexPath: toIndexPath: 

UITableViewDelegate

編輯表格行 (Editing Table Rows)
//開(kāi)始編輯某個(gè)cell時(shí)觸發(fā)
- tableView: willBeginEditingRowAtIndexPath: 
//完成編輯某行時(shí)觸發(fā)
- tableView: didEndEditingRowAtIndexPath:

//返回值決定了該cell的編輯狀態(tài) 
- (UITableViewCellEditingStyle)tableView: editingStyleForRowAtIndexPath: 
//返回值將作為刪除指定cell時(shí)確定按鈕的文本 
- (NSString *)tableView: titleForDeleteConfirmationButtonForRowAtIndexPath: 

//返回的BOOL值決定指定cell處于編輯狀態(tài)時(shí),該cell是否應(yīng)該縮進(jìn)
- (BOOL)tableView: shouldIndentWhileEditingRowAtIndexPath: 

實(shí)例

@interface TableViewController () {
    // 記錄當(dāng)前正在執(zhí)行的操作. 0代表刪除, 1代表插入
    NSInteger _action;
    NSMutableArray *_list;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *insertBtn;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *deleteBtn;
@end

@implementation ViewController

- (IBAction)Edit:(UIBarButtonItem *)sender {
    
    if ([sender.title isEqualToString:@"刪除"]) {
        _action = 0;
    } else {
        _action = 1;
    }
    
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
    
    if (self.tableView.isEditing) {
        self.insertBtn.title = @"完成";
        self.deleteBtn.title = @"完成";
    } else {
        self.insertBtn.title = @"插入";
        self.deleteBtn.title = @"刪除";
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _list = [NSMutableArray arrayWithObjects:@"美國(guó)", @"日本", @"中國(guó)", @"韓國(guó)", @"朝鮮", @"英國(guó)", nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuseID = @"reuseID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    
    cell.textLabel.text = _list[indexPath.row];
    return cell;
}

// 返回值決定指定indexPath對(duì)應(yīng)的cell是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 2) return NO;
    return YES;
}

// 返回值決定指定indexPath對(duì)應(yīng)的cell是否可移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 該方法告訴DataSource將指定位置的行移動(dòng)到另一個(gè)位置
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    // 獲取將要移動(dòng)的數(shù)據(jù)
    id targetObj = _list[sourceIndexPath.row];
    // 從集合中刪除指定數(shù)據(jù)項(xiàng)
    [_list removeObjectAtIndex:sourceIndexPath.row];
    // 將移動(dòng)的數(shù)據(jù)項(xiàng)插入到指定位置
    [_list insertObject:targetObj atIndex:destinationIndexPath.row];
}

// 編輯完成時(shí)觸發(fā)該方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        // 插入操作
        // 將數(shù)據(jù)插入到集合中
        [_list insertObject:_list[indexPath.row] atIndex:indexPath.row + 1];
        // 在界面上插入一行
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        
    } else {
        // 刪除操作
        // 從集合中刪除數(shù)據(jù)項(xiàng)
        [_list removeObjectAtIndex:indexPath.row];
        // 從界面上刪除指定行
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark - UITableViewDelegate
// 返回值決定了該cell的編輯狀態(tài)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _action == 0 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}

// 返回值將作為刪除指定cell時(shí)確定按鈕的文本
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"確定刪除";
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌闪萄,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件耸黑,死亡現(xiàn)場(chǎng)離奇詭異桃煎,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)大刊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門为迈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)三椿,“玉大人,你說(shuō)我怎么就攤上這事葫辐∷衙蹋” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵耿战,是天一觀的道長(zhǎng)蛋叼。 經(jīng)常有香客問(wèn)我,道長(zhǎng)剂陡,這世上最難降的妖魔是什么狈涮? 我笑而不...
    開(kāi)封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮鸭栖,結(jié)果婚禮上歌馍,老公的妹妹穿的比我還像新娘。我一直安慰自己晕鹊,他們只是感情好松却,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著溅话,像睡著了一般晓锻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上飞几,一...
    開(kāi)封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天砚哆,我揣著相機(jī)與錄音,去河邊找鬼循狰。 笑死窟社,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的绪钥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼关炼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼程腹!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起儒拂,我...
    開(kāi)封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤寸潦,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后社痛,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體见转,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年蒜哀,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了斩箫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖乘客,靈堂內(nèi)的尸體忽然破棺而出狐血,到底是詐尸還是另有隱情,我是刑警寧澤易核,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布匈织,位于F島的核電站,受9級(jí)特大地震影響牡直,放射性物質(zhì)發(fā)生泄漏缀匕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一碰逸、第九天 我趴在偏房一處隱蔽的房頂上張望乡小。 院中可真熱鬧,春花似錦花竞、人聲如沸劲件。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)零远。三九已至,卻和暖如春厌蔽,著一層夾襖步出監(jiān)牢的瞬間牵辣,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工奴饮, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留纬向,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓戴卜,卻偏偏與公主長(zhǎng)得像逾条,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子投剥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

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