快速使用用objective-c编写的委托方法

快速使用用objective-c编写的委托方法

问题描述:

我想使用在Swift中用Objecive-C编写的委托方法.该方法包含在 MGSwipeTableCell 框架(MGSwipeTableCell.h)中.

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

Objective-C:

Objective-C:

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

我尝试将其转换为swift并使用以下方法:

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
}

但是我不知道为什么,但是函数没有被调用.我有什么事吗我只想使用此功能获取滑动单元格的indexPath.

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.

您应该首先在表视图控制器中实现 MGSwipeTableCellDelegate 协议.所以你可以这样写:

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
    }
}

,然后在 cellForRowAtIndexPath:方法中创建单元格时,应按以下方式创建它:

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
}

然后,您将能够跟踪何时调用了swipe方法,因为您设置了单元委托属性.

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