0

I'd like to use a delegate method written in Objective-C in Swift. The method is included in the MGSwipeTableCell framework (MGSwipeTableCell.h).

Objective-C:

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

I try to convert it into Swift to and use the method:

func swipeTableCell(cell:MGSwipeTableCell, index:Int,  direction:MGSwipeDirection, fromExpansion:Bool) -> Bool {
    
    return true
}

But I don't know why but the function isn't getting called. Did I something wrong? I just want to get the indexPath of the swiped cell with this function.

2 Answers 2

0

You should implement MGSwipeTableCellDelegate protocol in your table view controller first. So you can just write:

class TableViewController : UITableViewController, MGSwipeTableCellDelegate {
    ....
    ....
    ....
    func swipeTableCell(cell:MGSwipeTableCell, index:Int,  direction:MGSwipeDirection, fromExpansion:Bool) -> Bool {
        return true
    }
}

and then when creating cells in cellForRowAtIndexPath: method you should create it like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  {
    let reuseIdentifier = "cell"
    var cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell!
    if cell == nil {
      cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier)
    }
    cell.delegate = self
    return cell
}

Then you'll be able to track when swipe method is called because you set the cell delegate property.

2
  • Thanks for your answer! It works. But now I set a detailTextLabel with this gesture/swipeTableCell function. Don't know why but it's not displayed in the cell. I think that it has something to do with the subclass of the tableView (MGSwipeTableCell). Hope you can help me. Nov 16, 2015 at 16:51
  • I think you should probably change style of the cell when you're creating it in cellForRowAtIndexPath:. Current style is set to .Subtitle, it should be .Default. Nov 16, 2015 at 17:07
0

It appears that the library you're trying to use has not adopted Objective-C nullability annotations yet, as such, any return values or arguments which are objects will be translated into Swift as implicitly unwrapped optionals (with the exclamation mark).

So, the signature you're looking for is this:

func swipeTableCell(cell: MGSwipeTableCell!, tappedButtonAtIndex: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool

But with that said, you need to add the protocol conformance anyway. If you do that first then try to write this method out, it should autocomplete to exactly how Swift expects it to look.

And then just make sure you're actually setting the cell's delegate property to whatever object is implement this method.

1
  • Thanks for your answer! It works. But now I set a detailTextLabel with this gesture/swipeTableCell function. Don't know why but it's not displayed in the cell. I think that it has something to do with the subclass of the tableView (MGSwipeTableCell). Hope you can help me. Nov 16, 2015 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.