【问题标题】:How to Run a Specific delegate For iOS Version?如何为 iOS 版本运行特定委托?
【发布时间】:2018-01-17 09:38:18
【问题描述】:
   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 上无论如何都不能运行来实现这个功能呢?

【问题讨论】:

  • 你项目的目标是什么iOS版本?
  • ios 9 是目标
  • And.. 你是直接在 UIViewController 子类上定义estimatedHeightForRowAt 方法还是使用符合UITableViewDelegate 的自定义类?
  • 最后一个问题:“在 Ios 10 中,此功能将运行,我的情况将受到干扰”是什么意思——表格视图是否显示高度错误的单元格?
  • 是的,如果此功能在 iOS 10 中运行,我的手机会受到干扰

标签: ios iphone swift uitableview ios11


【解决方案1】:

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

要解决此问题,您可以创建委托类的 iOS11 特定子类,并在表格视图上设置它时检查 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
  }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多