在 Swift 3 中调用 Objective-C 函数 [英] Call Objective-C function in Swift 3
本文介绍了在 Swift 3 中调用 Objective-C 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在嵌入 JBParallaxCell
,一个 UITableViewCell
子类.我想调用一个函数:
I am embeding JBParallaxCell
, a UITableViewCell
subclass. I want to call a function:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// Get visible cells on table view.
NSArray *visibleCells = [self.tableView visibleCells];
for (JBParallaxCell *cell in visibleCells) {
[cell cellOnTableView:self.tableView didScrollOnView:self.view];
}
}
我将此代码转换为 Swift:
I converted this code to Swift:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let visibleCells = table.visibleCells
var cells : JBParallaxCell?
for cells in visibleCells {
cells(on: table, didScrollOn: self.view)
// cells.cellOnTableView(tableView: table, didScrollOn: self.view)
}
}
他们给出的错误调用不是UITableViewCell
They give error call not function of UITableViewCell
推荐答案
如果你的 tableview outlet 叫做 table
,那么你可以这样做:
If your tableview outlet is called table
, then you'd could do:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for cell in table.visibleCells {
if let cell = cell as? JBParallaxCell {
cell.cell(on: table, didScrollOn: view)
}
}
}
或者,等价的:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
for cell in table.visibleCells {
(cell as? JBParallaxCell)?.cell(on: table, didScrollOn: view)
}
}
这篇关于在 Swift 3 中调用 Objective-C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文