如何 运行 iOS 版本的特定委托?

How to Run a Specific delegate For iOS Version?

   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

}

我只想要 运行s 的函数 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
        }

}

它给出错误:-

我知道我需要 return 一些东西但是如果我 return 0.0 或 UITableViewAutomaticDimension 比 Ios 10 此功能将 运行 我的情况会受到干扰。

那么如何 运行 这个函数只用于 ios 11 而它不会 运行 在 ios 10 上实现呢?

首先,要清除您的错误消息:在您的第一个示例中,您在方法上方添加了 @available(iOS 11.0, *);这并不意味着 仅在 iOS 11 上使用此方法,而是 此方法仅在 iOS 11 上可用。显然,编译器无法处理具有和不具有协议方法一致性的这种歧义("Schrödinger's Method",有人吗?)。

要解决此问题,您可以为您的委托 class 创建一个 iOS11-specific subclass 并在设置时检查 iOS 版本table 视图:

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
  }
}

这不是一个非常优雅的解决方案,但它应该可行。