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)文章
iOS開發(fā)中TabBar再次點(diǎn)擊實(shí)現(xiàn)刷新效果
這篇文章主要介紹了iOS開發(fā)中TabBar再次點(diǎn)擊實(shí)現(xiàn)刷新效果,實(shí)現(xiàn)方法也很簡(jiǎn)單,需要的朋友可以參考下2018-04-04
IOS中UIWebView加載Loading的實(shí)現(xiàn)方法
最近有朋友問我類似微信語(yǔ)音播放的喇叭動(dòng)畫和界面圖片加載loading界面是怎樣實(shí)現(xiàn)的,是不是就是一個(gè)gif圖片呢!我的回答當(dāng)然是否定了,當(dāng)然不排除也有人用gif圖片?。?/div> 2015-05-05
iOS應(yīng)用設(shè)計(jì)模式開發(fā)中對(duì)簡(jiǎn)單工廠和工廠方法模式的運(yùn)用
這篇文章主要介紹了iOS應(yīng)用設(shè)計(jì)模式開發(fā)中對(duì)簡(jiǎn)單工廠和工廠方法模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS?UITextView?實(shí)現(xiàn)類似微博的話題、提及用戶效果
這篇文章主要介紹了iOS?UITextView?實(shí)現(xiàn)類似微博的話題、提及功能,基本思路是使用正則匹配出成對(duì)的#,再利用UITextView的富文本實(shí)現(xiàn)高亮效果,需要的朋友可以參考下2022-06-06
簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù)
這篇文章主要介紹了簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù) 的相關(guān)資料,需要的朋友可以參考下2018-02-02
iOS表情鍵盤的簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了iOS表情鍵盤的簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
iOS實(shí)現(xiàn)數(shù)字倍數(shù)動(dòng)畫效果
在iOS開發(fā)中,制作動(dòng)畫效果是最讓開發(fā)者享受的環(huán)節(jié)之一,下面這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)數(shù)字倍數(shù)動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02最新評(píng)論

