user1960169 user1960169 - 2 months ago 8
iOS Question

How to set target for MGSwipeButton in iOS

Hello I am using MGSwipeTableCell for my

UITableview
in order to swipe my cells. Swiping is working. And I have added these lines in my
CellforRowAtIndex
like below

//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;


Now I want to set target click events for those 2 buttons. But I don't understand properly how to do this. Please help me with this.
Thanks

Answer

In order to listen for button click events you can implement the optional MGSwipeTableCellDelegate, or if you are too lazy to do that, the MGSwipeButton class comes with a convenience block callback ;)

Objective-c

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

Swift

MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
  (sender: MGSwipeTableCell!) -> Bool in
      println("Convenience callback for swipe buttons!")
      return true
})
Comments