gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:55:10 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com