MortimerGoro/MGSwipeTableCell

Use UIView as MGSwipeButton

damikon8 opened this issue · 2 comments

I have fully working tableView with cells and I would like to customize MGSwipeButton according to docs:

Using this class is optional because MGSwipeTableCell is button agnostic and can use any UIView for that purpose

I have something like this

class TurnOffAlarmSwipeButton: UIView {

  var callback: MGSwipeButtonCallback?

  override init(frame: CGRect){
    super.init(frame: frame)
    commonSetup()
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonSetup()
  }


  private func commonSetup() {    
    self.backgroundColor = .black
    self.callback = { cell -> Bool in
      print("ButtonTapped")
      return true
    }
  }

  @objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("Button Clicked")
  }

}

And listController:

class ListController: UITableViewController, UISplitViewControllerDelegate,MGSwipeTableCellDelegate {
   ...


  func swipeTableCell(_ cell: MGSwipeTableCell, tappedButtonAt index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
    print("Tapped as fck")
    return false
  }

  internal func getSwipeRightButtons(cell: MGSwipeTableCell) -> [UIView] {
    guard let indexPath = self.tableView.indexPath(for: cell) else {
      return []
    }

    let item = self.getItemByIndexPath(indexPath)

    let edit = MGSwipeButton(title: "ActiveAlarms_ContextMenu_DeactivateAlarm".localized(), icon: #imageLiteral(resourceName: "TurnOffAlarmIcon"), backgroundColor: UIColor.landaxColors.gray.dark, callback: { (cell) -> Bool in

      self.presentAlert(titleKey: "ActiveAlarmDeatils_PopUp_AlarmDeactivation_Title".localized(), messageKey: "ActiveAlarmDeatils_PopUp_AlarmDeactivation_Info".localized(), actions: [
        ("Alert_OkClose".localized(), nil),
        ("Alert_OkButton".localized(), { (action) in
          self.presentSimplePopover(message: "alarm for \n\n \(item.self) \n\n was activated? this dialog inform that we should get moved to activealarm details :) ")
        })
        ])

      return true
    })

    let customEdit = TurnOffAlarmSwipeButton()

    return [customEdit,edit]
  }
   ...
}

One button is correctly handled in the method func swipeTableCell(_ cell: MGSwipeTableCell, tappedButtonAt index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool

But that one of type UIView is not hitting this function. Any ideas? Is this even possible? This button act like clicking on cell (open details)

I had a similar requirement. For this purpose, I created a different custom views with xib with its superclass as MGSwipeButton. So, in my CustomView.xib I set up the views and my custom class looks like

class CustomView: MGSwipeButton {
override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }   
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    private func commonInit(){
        Bundle.main.loadNibNamed(“CustomView”, owner: self, options: nil)
        addSubview(myView)
        myView = self.bounds
        myView = [.flexibleHeight,.flexibleWidth]
    }
}

And in cellForRowAt method set your left or right button like

let leftButton = CustomView(frame: CGRect(x: 0, y: 0, width: 94, height: 155))
let leftButton2 = MGSwipeButton.init(title: “My Title” , backgroundColor: UIColor.red)
cell.leftButtons =  [leftButton, leftButton2]

You will get the tap event as required in

func swipeTableCell(_ cell: MGSwipeTableCell, tappedButtonAt index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
        
        guard let indexPath = gameListTableView.indexPath(for: cell) else {return true}

        switch index {
        case 0:
            //My CustomView tapped
            break
        case 1:
            //MGSwipe Button tapped
            break
        default:
            break
        
        return true
        
        
    }

Try changing your UIView to MGSwipeButton and see if that works.