func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
let chatMessageStarModel = arrMentionChat[indexPath.row]
if let key = chatMessageStarModel.chatMessageMarkerID?.stringValue() {
if let valueHeight = cellHeight[key] , valueHeight > 0.0{
return valueHeight
}
}
else {
return UITableViewAutomaticDimension
}
return UITableViewAutomaticDimension
}
我希望该函数 estimatedHeightForRowAt 仅适用于 Ios 11 或更高版本不适用于 Ios 10 或任何其他较低版本。如果我使用
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
let chatMessageStarModel = arrMentionChat[indexPath.row]
if let key = chatMessageStarModel.chatMessageMarkerID?.stringValue() {
if let valueHeight = cellHeight[key] , valueHeight > 0.0{
return valueHeight
}
}
else {
return UITableViewAutomaticDimension
}
return UITableViewAutomaticDimension
}
我尝试的另一种方法是:-
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
if #available(iOS 11.0,*) {
let chatMessageStarModel = arrMentionChat[indexPath.row]
if let key = chatMessageStarModel.chatMessageMarkerID?.stringValue() {
if let valueHeight = cellHeight[key] , valueHeight > 0.0{
return valueHeight
}
}
else {
return UITableViewAutomaticDimension
}
return UITableViewAutomaticDimension
}
}
我知道我需要返回一些东西但是如果我返回 0.0 或 UITableViewAutomaticDimension 而不是在 Ios 10 中这个函数将运行并且我的案例会受到干扰。
那么如何只在 ios 11 上运行这个函数,而它不在 ios 10 上运行来实现这个?
最佳答案
首先,要清除您的错误消息:在您的第一个示例中,您在方法上方添加了 @available(iOS 11.0, *)
;这并不意味着只能在 iOS 11 上使用此方法,而是此方法仅在 iOS 11 中可用。显然,编译器无法处理具有和不具有协议(protocol)方法一致性(“薛定谔方法”,任何人?)的这种歧义。
要解决此问题,您可以创建委托(delegate)类的特定于 iOS11 的子类,并在 TableView 上设置它时检查 iOS 版本:
class Delegate: NSObject, UITableViewDelegate {
// ... your version-independend delegate code goes here...
}
@available(iOS 11.0, *)
class IOS11Delegate: Delegate {
// ... the iOS11-specific stuff goes here ...
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
// ... your custom height estimation code goes here ...
}
}
class ViewController: UITableViewController {
private var delegate: Delegate?
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 11.0,*) {
delegate = IOS11Delegate()
}
else {
delegate = Delegate()
}
tableView.delegate = delegate
}
}
这不是一个非常优雅的解决方案,但它应该可以工作。
关于ios - 如何为 iOS 版本运行特定委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48297827/