MGSwipeTableCell的使用

MGSwipeTableCell的使用

 

  側滑UITableViewCell展現多個可操做按鈕是iOS開發中經常使用到的一個功能。這裏有個很是強大的開源庫:MGSwipeTableCell,能夠實現此功能。其效果以下圖所示:ios

示意圖.gifgit

引入工程

■CocoaPods 引入
  建議使用,沒有使用過CocoaPods的童鞋能夠參照大神唐巧的這篇文章用CocoaPods作iOS程序的依賴管理github

■手動引入
  下載地址MGSwipeTableCell數組

使用

  能夠直接用MGSwipeTableCellcell,也能夠繼承於MGSwipeTableCell,下面是一個使用的例子:app

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuseIdentifier = @"programmaticCell";
    MGSwipeTableCell * cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell)
    {
        cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }

    cell.textLabel.text = @"Title";
    cell.detailTextLabel.text = @"Detail text";
    cell.delegate = self; //optional

    //configure left buttons
    cell.leftButtons = @[[MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"check.png"] backgroundColor:[UIColor greenColor]],
                         [MGSwipeButton buttonWithTitle:@"" icon:[UIImage imageNamed:@"fav.png"] backgroundColor:[UIColor blueColor]]];

    cell.leftSwipeSettings.transition = MGSwipeTransition3D;

    //configure right buttons
    cell.rightButtons = @[[MGSwipeButton buttonWithTitle:@"Delete" backgroundColor:[UIColor redColor]],
                          [MGSwipeButton buttonWithTitle:@"More" backgroundColor:[UIColor lightGrayColor]]];
    cell.rightSwipeSettings.transition = MGSwipeTransition3D;
    return cell;
}

  cell.leftButtons 和 cell.rightButtons都是一個數組,能夠動態填寫按。 動畫

  cell.rightSwipeSettings.transition是側滑的動畫,有多重選擇。atom

  爲了監聽點擊按鈕的事件,你有兩種方法:spa

  ■實現MGSwipeTableCellDelegatecode

  ■定義MGSwipeButton按鈕時有方便使用的block回調:blog

[MGSwipeButton buttonWithTitle:@"More"
                   backgroundColor:[UIColor lightGrayColor]
                          callback:^BOOL(MGSwipeTableCell *sender) {
                              NSLog(@"Convenience callback for swipe buttons!");
                          }];

Delegate

  MGSwipeTableCellDelegate 是用來配置滑動按鈕或接收觸發的動做或另外一個事件的可選的委託。按鈕能夠在cell建立的時候就嵌入進去而不用使用delegate,不過使用delegate能夠改善內存的使用,由於按鈕只有在使用的時候才建立。

- (BOOL)swipeTableCell:(MGSwipeTableCell*)cell canSwipe:(MGSwipeDirection)direction;

  決定是否能夠使用划動手勢。

- (void)swipeTableCell:(MGSwipeTableCell*)cell
   didChangeSwipeState:(MGSwipeState)state
       gestureIsActive:(BOOL) gestureIsActive;

  當前swipe state狀態改變時使用。

- (BOOL)swipeTableCell:(MGSwipeTableCell*)cell
   tappedButtonAtIndex:(NSInteger)index
             direction:(MGSwipeDirection)direction
         fromExpansion:(BOOL) fromExpansion;

  用戶點擊按鈕時回調。

- (NSArray*)swipeTableCell:(MGSwipeTableCell*)cell
  swipeButtonsForDirection:(MGSwipeDirection)direction
             swipeSettings:(MGSwipeSettings*)swipeSettings
         expansionSettings:(MGSwipeExpansionSettings*)expansionSettings;

  設置swipe button 和 swipe/expansion 的設置。

可擴展的按鈕(Expandable buttons)

  按鈕默認是不能夠擴展的。你能夠使用cell.leftExpansion 和 cell.rightExpansion 來設置可可擴展的按鈕。

  可擴展的按鈕事件是當用戶完成滑動手勢時自動觸發的。擴展程度是動態的(經過threshold的值來設置)。觸發的可擴展按鈕能夠恢復到其初始位置或填充整個 UITableViewCell,您能夠使用 fillOnTrigger 屬性選擇所需的動畫。

@interface MGSwipeExpansionSettings: NSObject

/** index of the expandable button (in the left or right buttons arrays) */
@property (nonatomic, assign) NSInteger buttonIndex;

/** if true the button fills the cell on trigger, else it bounces back to its initial position */
@property (nonatomic, assign) BOOL fillOnTrigger;

/** Size proportional threshold to trigger the expansion button. Default value 1.5 */
@property (nonatomic, assign) CGFloat threshold;

@end
相關文章
相關標籤/搜索