修复了 tableView 函数获取 "deprecated and will be removed in Swift 4" 警告的问题

Fix for getting "deprecated and will be removed in Swift 4" warning for tableView functions

我刚刚将我的代码升级到 Swift 4,现在在控制台上收到所有 tableView 函数的警告。

tableView:willDisplayCell:forRowAtIndexPath:] is deprecated and will be removed in Swift 4; add explicit '@objc' to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message

tableView:cellForRowAtIndexPath:] is deprecated and will be removed in Swift 4

我不推荐任何人像这样解决这个问题,但它确实对我有用。 您可以在您使用的每个 tableView 函数的开头添加“@objc”。

像这样:

@objc func tableView(_ tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return 1
}

使用下面的代码 cellForRow

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
}

使用下面的一个来识别正在显示

的单元格
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
}

如果您对数据源和委托函数有疑问 select UITableViewDataSource 或 UITableViewDelegate 并右键单击跳转到定义,这将导致获取您的函数

希望对您有所帮助