ios - 如何为 iOS 版本运行特定委托(delegate)?

标签 ios iphone swift uitableview ios11

   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

}

显示错误 enter image description here

我尝试的另一种方法是:-

  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
        }

}

它给出错误:- enter image description here

我知道我需要返回一些东西但是如果我返回 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/

相关文章:

ios - 在 iOS 中,动画 View 在手动调用 segue 后返回到其原始状态

IOS Facebook SDK 3.17 : Doesn't give me publish_stream permissions (while older versions as well as android both work -- so it is sdk specific)

android - 通知 iOS 和 Android 服务器上的数据更改

ios - 通过代码设置 WKInterfaceImage 的(边界)半径

iphone - 避免 iPhone 应用程序崩溃的提示和技巧

iphone - 有没有办法从 iPhone 地址簿 API 获取 "Me"卡?

ios - 如何在与另一个不同的 View Controller 上设置 bool 标志?

ios - iPhone 的部分模态视图和缩小呈现 View

ios - 单元格维度 rowHeight 大小

swift - swift 中 == true 和 != false 之间有区别吗?