iOS應(yīng)用中UITableView左滑自定義選項(xiàng)及批量刪除的實(shí)現(xiàn)
實(shí)現(xiàn)UITableView左滑自定義選項(xiàng)
當(dāng)UITableView進(jìn)入編輯模式,在進(jìn)行左滑操作的cell的右邊,默認(rèn)會(huì)出現(xiàn)Delete按鈕,如何自定義左滑出現(xiàn)的按鈕呢?
只需要實(shí)現(xiàn)UITableView下面的這個(gè)代理方法。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *likeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"喜歡" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 實(shí)現(xiàn)相關(guān)的邏輯代碼
// ...
// 在最后希望cell可以自動(dòng)回到默認(rèn)狀態(tài),所以需要退出編輯模式
tableView.editing = NO;
}];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// 首先改變model
[self.books removeObjectAtIndex:indexPath.row];
// 接著刷新view
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// 不需要主動(dòng)退出編輯模式,上面更新view的操作完成后就會(huì)自動(dòng)退出編輯模式
}];
return @[deleteAction, likeAction];
}
此時(shí)左滑就會(huì)出現(xiàn)兩個(gè)按鈕,一個(gè)是喜歡,另一個(gè)是刪除。出現(xiàn)的順序和在這個(gè)方法中返回的數(shù)組中的元素順序相關(guān)。
如果實(shí)現(xiàn)了上述方法,那么之前提到過的tableView:commitEditingStyle:forRowAtIndexPath:和tableView: titleForDeleteConfirmationButtonForRowAtIndexPath:方法就不會(huì)再調(diào)用了。(如果為了兼容以前的版本,那么需要實(shí)現(xiàn)tableView:commitEditingStyle:forRowAtIndexPath:方法,在這個(gè)方法里什么都不用做即可。)
UITableview的多行同時(shí)刪除
下面這段代碼配合xib使用, 不過關(guān)鍵不在這地方,記住后面的使用到的委托。
其實(shí)質(zhì)就是數(shù)組array的刪除操作。
//
// UITableViewDelteMutilRowsViewController.m
// UITableViewDelteMutilRows
//
#import "UITableViewDelteMutilRowsViewController.h"
@implementation UITableViewDelteMutilRowsViewController
@synthesize tableview;
@synthesize dataArray;
@synthesize deleteDic;
@synthesize leftButton;
@synthesize rightButton;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
deleteDic = [[NSMutableDictionary alloc] init];
rightButton.title = @"編輯";
}
- (IBAction)choseData{
if (rightButton.title == @"編輯") {
rightButton.title = @"確定";
[self.tableview setEditing:YES animated:YES];
}
else {
rightButton.title = @"編輯";
[deleteDic removeAllObjects];
[self.tableview setEditing:NO animated:YES];
}
}
- (IBAction)deleteFuntion{
[dataArray removeObjectsInArray:[deleteDic allKeys]];
[self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithArray:[deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade];
[deleteDic removeAllObjects];
}
- (void)dealloc {
[leftButton release];
[rightButton release];
[deleteDic release];
[dataArray release];
[tableview release];
[super dealloc];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [dataArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
return cell;
}
/*//這里設(shè)置為可滑動(dòng)編輯刪除
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (rightButton.title== @"確定") {
[deleteDic setObject:indexPath forKey:[dataArray objectAtIndex:indexPath.row]];
}
else {
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (rightButton.title == @"確定") {
[deleteDic removeObjectForKey:[dataArray objectAtIndex:indexPath.row]];
}
}
@end
- iOS開發(fā)之UITableView左滑刪除等自定義功能
- 全面解析iOS應(yīng)用中自定義UITableViewCell的方法
- iOS中UITableView Cell實(shí)現(xiàn)自定義單選功能
- 詳解ios中自定義cell,自定義UITableViewCell
- iOS App開發(fā)中使用及自定義UITableViewCell的教程
- 實(shí)例講解iOS應(yīng)用開發(fā)中使用UITableView創(chuàng)建自定義表格
- ios UITableView 自定義右滑刪除的實(shí)現(xiàn)代碼
- iOS自定義UITableView實(shí)現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解
相關(guān)文章
Observing?KVO?Key-Value基本使用原理示例詳解
這篇文章主要為大家介紹了Observing?KVO?Key-Value基本使用原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08替代pod update速度慢的lg_pod_plugin安裝使用詳解
這篇文章主要介紹了替代pod update速度慢lg_pod_plugin安裝使用方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09IOS數(shù)字鍵盤左下角添加完成按鈕的實(shí)現(xiàn)方法
這篇文章主要介紹了IOS數(shù)字鍵盤左下角添加完成按鈕的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能實(shí)現(xiàn)類似這樣的功能,需要的朋友可以參考下2017-08-08移動(dòng)端固定輸入框在底部會(huì)被鍵盤遮擋的解決方法(必看篇)
下面小編就為大家分享關(guān)于移動(dòng)端固定輸入框在底部會(huì)被鍵盤遮擋的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12簡(jiǎn)單好用可任意定制的iOS Popover氣泡效果
Popover(氣泡彈出框/彈出式氣泡/氣泡)是由一個(gè)矩形和三角箭頭組成的彈出窗口,箭頭指向的地方通常是導(dǎo)致Popover彈出的控件或區(qū)域。本文通過實(shí)例代碼給大家介紹了iOS Popover氣泡效果,需要的朋友參考下吧2017-12-12iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫
這篇文章主要介紹了iOS開發(fā)中使用FMDB來使程序連接SQLite數(shù)據(jù)庫,SQLite是一個(gè)簡(jiǎn)單的嵌入式數(shù)據(jù)庫,非常適合輕量級(jí)使用,需要的朋友可以參考下2015-11-11iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長(zhǎng)按保存圖片的示例
本篇文章主要介紹了iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長(zhǎng)按保存圖片的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)
這篇文章主要介紹了iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié),需要的朋友可以參考下2017-11-11iOS開發(fā)中不合法的網(wǎng)絡(luò)請(qǐng)求地址如何解決
這篇文章主要介紹了iOS開發(fā)中不合法的網(wǎng)絡(luò)請(qǐng)求地址的解決方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09