objective-c - 在 Swift 代码中使用 Objective-C 编写的委托(delegate)方法

标签 objective-c swift interop

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

Objective-C :

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

我尝试将其转换为 Swift 并使用该方法:

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

但我不知道为什么,但该函数没有被调用。我有什么错吗?我只想用这个函数获取滑动单元格的indexPath。

最佳答案

您应该首先在 TableView Controller 中实现 MGSwipeTableCellDelegate 协议(protocol)。所以你可以写:

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

然后在 cellForRowAtIndexPath: 方法中创建单元格时,您应该像这样创建它:

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
}

然后,您将能够跟踪何时调用滑动方法,因为您设置了单元格委托(delegate)属性。

关于objective-c - 在 Swift 代码中使用 Objective-C 编写的委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33725173/

相关文章:

ios - 如何在 Swift 中构造多个 HTTP 请求?

.Net - 用 .Net 编写的类库 (dll) 可以由用 C 或 C++ 编写的应用程序使用吗?

c# - 在 SSIS 中使用 Excel 互操作的问题

ios - "Use of undeclared identifier"即使链接了框架并导入了头文件

iphone - NSString 变量在整个程序中可用

ios - 如何将iOS键盘 "go"按钮与UIButton关联?

ios - 重新加载 ViewController

ios - NSURLCache - 它什么时候过期?

ios - Inout 参数不允许互相使用别名

c# - 同一 COM 库中的 C++ 和 C# 类