在 Swift 代码中使用 Objective-C 中编写的委托方法
Use delegate method written in Objective-C in Swift code
我想在 Swift 中使用 Objective-C 中编写的委托方法。该方法包含在 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。
您应该首先在 table 视图控制器中实施 MGSwipeTableCellDelegate
协议。所以你可以写:
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
}
然后您将能够跟踪何时调用滑动方法,因为您设置了单元格委托 属性。
您尝试使用的库似乎尚未采用 ,因此,作为对象的任何 return 值或参数都将被翻译成 Swift作为隐式展开的选项(带有感叹号)。
那么,您要找的签名是这样的:
func swipeTableCell(cell: MGSwipeTableCell!, tappedButtonAtIndex: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool
但是话虽如此,您仍然需要添加协议一致性。如果你先这样做,然后尝试写出这个方法,它应该自动完成 Swift 期望它看起来的样子。
然后确保您确实将单元格的 delegate
属性 设置为实现此方法的任何对象。
我想在 Swift 中使用 Objective-C 中编写的委托方法。该方法包含在 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。
您应该首先在 table 视图控制器中实施 MGSwipeTableCellDelegate
协议。所以你可以写:
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
}
然后您将能够跟踪何时调用滑动方法,因为您设置了单元格委托 属性。
您尝试使用的库似乎尚未采用
那么,您要找的签名是这样的:
func swipeTableCell(cell: MGSwipeTableCell!, tappedButtonAtIndex: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool
但是话虽如此,您仍然需要添加协议一致性。如果你先这样做,然后尝试写出这个方法,它应该自动完成 Swift 期望它看起来的样子。
然后确保您确实将单元格的 delegate
属性 设置为实现此方法的任何对象。