iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解

前言

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設計、成都網(wǎng)站建設與策劃設計,肥鄉(xiāng)網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設10年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:肥鄉(xiāng)等地區(qū)。肥鄉(xiāng)做網(wǎng)站價格咨詢:18982081108

在我們的app開發(fā)當中,經(jīng)常會用到UITableView 的左滑刪除的功能,通常的話效果如下

iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解

但有時候系統(tǒng)現(xiàn)有的功能并不能完全滿足我們的開發(fā)需求,這樣就需要我們在其現(xiàn)有的功能基礎上自定義我們所需要的功能了。下圖是在項目中自定義的按鈕(只是修改了按鈕的frame而已)。

iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解

然后我就總結(jié)了一下根據(jù)不同的需求自定義不同的按鈕。

一、系統(tǒng)默認左滑刪除按鈕

如果你對左滑刪除按鈕的要求不高,僅僅只是實現(xiàn)UITableView上cell的左滑刪除功能,那在UITableView的代理方法中添加以下兩種方法便可實現(xiàn)需求:

//使用系統(tǒng)默認的刪除按鈕
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete){

 }
}
//自定義系統(tǒng)默認的刪除按鈕文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
 return @"自定義按鈕”;
}

效果如下所示:

iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解
系統(tǒng)自帶

雖然這樣能基本實現(xiàn)功能,但是我們發(fā)現(xiàn)右邊的按鈕和左邊的黃色區(qū)域的高度并不一樣。這是因為右邊按鈕是和UITableViewCell的高度一致,而左邊的黃色區(qū)域只是一張圖片而已,其高度設置和UITableViewCell的高度并不一致,才會導致這樣的布局出現(xiàn)。如果我們想要刪除按鈕和左邊圖片一樣的高度,那我們就需要自定義刪除按鈕的高度了。

二、自定義左滑刪除按鈕

如果我們想要實現(xiàn)不止一個自定義按鈕的功能,那我們就需要在UITableView代理方法- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {}中添加我們所需要的多個按鈕了。如下是在不同的cell上添加一個或兩個左滑按鈕:

//自定義多個左滑菜單選項
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewRowAction *deleteAction;
 deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
 [tableView setEditing:NO animated:YES];//退出編輯模式,隱藏左滑菜單
 }];
 if (indexPath.row == 1) {//在不同的cell上添加不同的按鈕
 UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"分享" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  [tableView setEditing:NO animated:YES];//退出編輯模式,隱藏左滑菜單
 }];
 shareAction.backgroundColor = [UIColor blueColor];
 return @[deleteAction,shareAction];
 }
 return @[deleteAction];
}

在上述代理方法中我們就可以實現(xiàn)在cell中添加一個或多個左滑按鈕了,根據(jù)點擊不同的按鈕實現(xiàn)不同的響應方法便可。其中[tableView setEditing:NO animated:YES];方法可以在點擊按鈕之后退出編輯模式并隱藏左滑菜單。但如果我們想要修改按鈕的其他屬性如標題、背景顏色怎么辦?點擊進入UITableViewRowAction類中,我們會發(fā)現(xiàn)以下屬性和方法:

@interface UITableViewRowAction : NSObject <NSCopying>

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

@property (nonatomic, readonly) UITableViewRowActionStyle style;
@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) UIColor *backgroundColor; // default background color is dependent on style
@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;

@end

其中 @property (nonatomic, readonly) UITableViewRowActionStyle style;是指設置所添加按鈕父視圖的背景顏色以及按鈕字體顏色:

typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
 UITableViewRowActionStyleDefault = 0,//紅底白字
 UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,
 UITableViewRowActionStyleNormal//灰底白字
} NS_ENUM_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

@property (nonatomic, copy, nullable) UIVisualEffect* backgroundEffect;提供了一個背景模糊效果,有興趣的可以自行研究一下。

上述的方法和屬性只能滿足我們的部分需求,如果我們想要改變按鈕的大小或者設置帶圖片的按鈕怎么辦?那就需要我們在視圖中找到我們所要修改的按鈕,并設置它的各種屬性。由于在iOS8-10和iOS11下自定義按鈕處在不同的視圖層次中,所以需要我們先了解UITableView上的視圖層次。下圖為對比:

iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解

左iOS10/右iOS11(Xcode9中)

從對比圖中可以看出:

(1).iOS10下視圖層次為:UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView -> _UITableViewCellActionButton,我們所需自定義的按鈕視圖UITableViewCellDeleteConfirmationView(左圖中紅框處)是UITableViewCell的子視圖。

(2).iOS11下視圖層次為:在Xcode 8中編譯為: UITableView -> UITableViewWrapperView -> UISwipeActionPullView -> UISwipeActionStandardButton;

在Xcode 9中編譯為: UITableView -> UISwipeActionPullView -> UISwipeActionStandardButton。(iOS11中用Xcode 8和Xcode 9中編譯有略微的差別),我們所需自定義的按鈕視圖UISwipeActionPullView(右圖中紅框處)是UITableView的子視圖。

由于不同系統(tǒng)下的視圖層次不一樣,因此我們在項目中需要根據(jù)不同的代碼去同時適配iOS8-10和iOS11。
在iOS8-10中( 以下均在Xcode 9中編譯):

在該系統(tǒng)下由于我們所需自定義的按鈕視圖UITableViewCellDeleteConfirmationView是UITableViewCell的子視圖,所以我們在自定義UITableViewCell子類中遍歷它的subviews即可。代碼如下:

- (void)layoutSubviews {
 /**自定義設置iOS8-10系統(tǒng)下的左滑刪除按鈕大小*/
 for (UIView * subView in self.subviews) {
 if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
  subView.backgroundColor = [UIColor clearColor];//去掉默認紅色背景
  //設置按鈕frame
  CGRect cRect = subView.frame;
  cRect.origin.y = self.contentView.frame.origin.y + 10;
  cRect.size.height = self.contentView.frame.size.height - 20;
  subView.frame = cRect;
  //自定義按鈕的文字大小
  if (subView.subviews.count == 1 && self.indexPath.section == 0) {//表示有一個按鈕
  UIButton * deleteButton = subView.subviews[0];
  deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
  }
  //自定義按鈕的圖片
  if (subView.subviews.count == 1 && self.indexPath.section == 1) {//表示有一個按鈕
  UIButton * deleteButton = subView.subviews[0];
  [deleteButton setImage:[UIImage imageNamed:@"login_btn_message"] forState:UIControlStateNormal];
  [deleteButton setTitle:@"" forState:UIControlStateNormal];
  }
  //自定義按鈕的文字圖片
  if (subView.subviews.count >= 2 && self.indexPath.section == 0) {//表示有兩個按鈕
  UIButton * deleteButton = subView.subviews[1];
  UIButton * shareButton = subView.subviews[0];
  [deleteButton setTitle:@"" forState:UIControlStateNormal];
  [shareButton setTitle:@"" forState:UIControlStateNormal];
  [self setUpDeleteButton:deleteButton];
  [self setUpShareButton:shareButton];
  }
 }
 }
}

在iOS11中:

在該系統(tǒng)下由于我們所需自定義的按鈕視圖UISwipeActionPullView是UITableView的子視圖,所以我們可以在控制器中自定義UITableView子類中遍歷它的subviews即可(以下方法是寫在UITableView的代理方法- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath中的,該代理方法每次會在開始左滑按鈕前調(diào)用)。代碼如下:

/**自定義設置iOS11系統(tǒng)下的左滑刪除按鈕大小*/
//開始編輯左滑刪除
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
 NSInteger section = indexPath.section;
 if (@available(iOS 11.0, *)) {
  for (UIView * subView in self.customTableView.subviews) {
   if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
    subView.backgroundColor = [UIColor clearColor];//如果自定義只有一個按鈕就要去掉按鈕默認紅色背景
    //設置按鈕frame
    for (UIView * sonView in subView.subviews) {
     if ([sonView isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
      CGRect cRect = sonView.frame;
      cRect.origin.y = sonView.frame.origin.y + 10;
      cRect.size.height = sonView.frame.size.height - 20;
      sonView.frame = cRect;
     }
    }
    //自定義按鈕的文字大小
    if (subView.subviews.count == 1 && section == 0) {//表示有一個按鈕
     UIButton * deleteButton = subView.subviews[0];
     deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    //自定義按鈕的圖片
    if (subView.subviews.count == 1 && section == 1) {//表示有一個按鈕
     UIButton * deleteButton = subView.subviews[0];
     [deleteButton setImage:[UIImage imageNamed:@"login_btn_message"] forState:UIControlStateNormal];;
    }
    //自定義按鈕的文字圖片
    if (subView.subviews.count >= 2 && section == 0) {//表示有兩個按鈕
     UIButton * deleteButton = subView.subviews[1];
     UIButton * shareButton = subView.subviews[0];
     [self setUpDeleteButton:deleteButton];
     [self setUpShareButton:shareButton];
    }
   }
  }
 }
}

如果我們想在左滑刪除結(jié)束后實現(xiàn)一些功能,我們可以在UITableView中實現(xiàn)以下代理方法:

//結(jié)束編輯左滑刪除
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {

}
如果我們想分別設置UITableViewCell是否需要實現(xiàn)左滑功能,可以在下面代理方法中實現(xiàn):

//判斷是否顯示左滑刪除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 return YES;
}

在不同系統(tǒng)下分別添加以上代碼即可實現(xiàn)我們所需要的自定義左滑刪除按鈕,效果圖如下:

iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解

以上是我總結(jié)整理的在不同系統(tǒng)下的自定義UITableView左滑刪除功能。

如有不足之處,歡迎指正交流,Demo地址:左滑刪除 (本地下載)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。

網(wǎng)站題目:iOS自定義UITableView實現(xiàn)不同系統(tǒng)下的左滑刪除功能詳解
瀏覽路徑:http://muchs.cn/article2/isjcoc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航微信公眾號、網(wǎng)頁設計公司、面包屑導航、商城網(wǎng)站企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設