代码之家  ›  专栏  ›  技术社区  ›  user1960169

如何在iOS中设置MGSwipeButton的目标

  •  1
  • user1960169  · 技术社区  · 10 年前

    你好,我正在使用 MGSwipeTableCell 为我的 UITableview 为了刷我的手机。刷卡是有效的。我在我的 CellforRowAtIndex 如下所示

     //configure left buttons
    cell.leftButtons = @[[MGSwipeButton buttonWithTitle:@"Approve" icon:[UIImage imageNamed:@"approveTick"] backgroundColor:[UIColor colorWithRed:43.0/255 green:178.0/255 blue:157.0/255 alpha:1.0]]
                         ];
    
    cell.leftSwipeSettings.transition = MGSwipeTransition3D;
    
    
    //configure right button
    cell.rightButtons=@[[MGSwipeButton buttonWithTitle:@"Reject" icon:[UIImage imageNamed:@"rejectDel"] backgroundColor:[UIColor colorWithRed:243.0/255 green:104.0/255 blue:97.0/255 alpha:1.0]]
                        ];
    
    cell.rightSwipeSettings.transition=MGSwipeTransition3D;
    

    现在我想为这两个按钮设置目标点击事件。但我不知道该怎么做。请帮我解决这个问题。 谢谢

    3 回复  |  直到 10 年前
        1
  •  0
  •   arpan.r    10 年前

    为了监听按钮点击事件,您可以实现可选的MGSwipeTableCellDelegate,或者如果您太懒了,MGSwipe button类附带了一个方便的块回调;)

    目标c

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

    敏捷的

    MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
      (sender: MGSwipeTableCell!) -> Bool in
          println("Convenience callback for swipe buttons!")
          return true
    })
    
        2
  •  0
  •   Chirag D jinjuwadiya    10 年前
    -(BOOL)swipeTableCell:(MGSwipeTableCell *)cell tappedButtonAtIndex:(NSInteger)index direction:(MGSwipeDirection)direction fromExpansion:(BOOL)fromExpansion {
        NSLog(@"tappedButtonAtIndex: %i",index);
        NSIndexPath *indexPath = [Claim_tableview indexPathForCell:cell];
        NSLog(@"CellindexPath: %i",indexPath);
        return YES;
    }
    

    此委托方法将在您单击按钮时调用。请不要忘记添加“MGSwipeTableCellDelegate”

        3
  •  0
  •   abanet    10 年前

    第一个答案是正确的 GitHub MGSwipeTableCell 但对我来说,我并没有使用swift 3,所以我决定与代表一起使用。

    这样做真的很容易。首先,我告诉我的班级我要实现MGSwipeTableCell协议。我用一个扩展来完成此操作:

    extension ViewController: MGSwipeTableCellDelegate {
        func swipeTableCell(_ cell: MGSwipeTableCell!, tappedButtonAt index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
            print("Button \(index) tapped")
            // here a switch to control the button tapped is a good choice
            return true
        }
    }
    

    第二:别忘了设置单元格。去角质 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    这对我来说是Xcode 8和swift 3!