gpt4 book ai didi

ios - 一般如何使用 iOS 14 单元格内容配置?

转载 作者:行者123 更新时间:2023-12-01 15:44:14 24 4
gpt4 key购买 nike

我正在观看并重新观看 WWDC 2020“现代单元配置”,但启发性并不大。
我知道内置的 ListContentConfiguration 具有类似于文本标签和 ImageView 等内置单元格样式组件的属性。但我从不使用内置的单元格样式;我总是创建一个单元子类,并在 xib 或 Storyboard原型(prototype)单元中从头开始设计单元 subview ,甚至在代码中构造它们。
那么如何使用 Apple 的配置来填充我的单元格的 subview ?
视频说:

In addition to the list content configuration we're also giving you access to the associated list content view which implements all of the rendering. You just create or update this view using the configuration, and then you can add it as a subview right alongside your own custom views. This lets you take advantage of all the content configuration features and combine the ListContentView with your own additional custom views next to it, such as an extra image view or a label.


好吧,不,这不是我想做的。我不想要任何内置的单元格样式 subview 。
然后视频说:

Even when you're building a completely custom view hierarchy inside your cells, you can still use the system configurations to help.Because configurations are so lightweight, you can use them as a source of default values for things like fonts, colors, and margins that you copy over to your custom views, even if you never apply the configuration directly itself.And for more advanced use cases you can create a completely custom content configuration type with a paired content view class that renders it, and then use your custom configuration with any cell the same way that you would use a list content configuration.


我的斜体,斜体是我要问的部分。我问:怎么做?
  • 我知道有一个UIContentConfiguration协议(protocol)。
  • 我了解符合类generates通过其makeContentView方法一个“内容 View ”,一个带有 configuration 的 UIView属性(因为它符合 UIContentConfiguration)。

  • 那么如何将它与我的自定义单元子类结合使用,将信息从数据源传递到单元并填充单元的 subview ?
    像往常一样,感觉就像 Apple 向我们展示了玩具示例,并完全省略了有关如何在现实世界中工作的细节。有没有人弄清楚这一点?

    最佳答案

    编辑 我现在已经发表了一系列关于这个主题的文章,从 https://www.biteinteractive.com/cell-content-configuration-in-ios-14/ 开始。 .

    这里的关键——我认为 Apple 在视频中根本没有明确说明这一点——这些单元配置的工作方式是从字面上撕掉单元的 contentView。并将其替换为配置提供的 View 作为其 makeContentView 的输出.
    因此,您所要做的就是手动构建整个内容 View ,运行时会为您将其放入单元格中。
    这是一个例子。我们需要提供自己的采用 UIContentConfiguration 的配置类型,这样我们就可以定义自己的属性;它还必须实现 makeContentView()updated(for:) .所以假设我们有四个文本要显示在单元格中:

    struct Configuration : UIContentConfiguration {
    let text1: String
    let text2: String
    let text3: String
    let text4: String
    func makeContentView() -> UIView & UIContentView {
    let c = MyContentView(configuration: self)
    return c
    }
    func updated(for state: UIConfigurationState) -> MyCell.Configuration {
    return self
    }
    }
    在现实生活中,我们可能会通过更改返回此配置的某些属性更改的版本来响应状态更改,但在这种情况下,没有什么可做的,所以我们只返回 self .
    我们假设存在 MyContentView,这是一个采用 UIContentView 的 UIView 子类,这意味着它有一个 configuration属性(property)。这是我们配置 View 的 subview 并应用配置的地方。在这种情况下,应用配置意味着只需设置四个标签的文本。我将这两个任务分开:
    class MyContentView: UIView, UIContentView {
    var configuration: UIContentConfiguration {
    didSet {
    self.configure()
    }
    }
    private let lab1 = UILabel()
    private let lab2 = UILabel()
    private let lab3 = UILabel()
    private let lab4 = UILabel()
    init(configuration: UIContentConfiguration) {
    self.configuration = configuration
    super.init(frame: .zero)
    // ... configure the subviews ...
    // ... and add them as subviews to self ...
    self.configure()
    }
    required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }
    private func configure() {
    guard let config = self.configuration as? Configuration else { return }
    self.lab1.text = config.text1
    self.lab2.text = config.text2
    self.lab3.text = config.text3
    self.lab4.text = config.text4
    }
    }
    您可以看到该架构的重点。如果在将来某个时候我们被分配了一个新的 configuration ,我们只需调用 configure再次设置标签的文本,无需重建 subview 本身。在现实生活中,我们可以通过检查传入的配置来获得更高的效率;如果与当前配置相同,则无需调用 self.configure()再次。
    结果是我们现在可以在我们的 tableView(_:cellForRowAt:) 中这样交谈。执行:
    override func tableView(_ tableView: UITableView, 
    cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(
    withIdentifier: self.cellID, for: indexPath) as! MyCell
    let config = MyCell.Configuration(
    text1: "Harpo",
    text2: "Groucho",
    text3: "Chico",
    text4: "Zeppo"
    )
    cell.contentConfiguration = config
    return cell
    }
    所有这些都非常聪明,但不幸的是,内容 View 界面似乎必须在代码中创建——我们无法从 nib 加载现成的单元格,因为从 nib 加载的内容 View 及其所有 subview , 将被我们的 makeContentView 返回的内容 View 替换执行。因此,Apple 的配置架构不能与您在 Storyboard 或 .xib 文件中设计的单元格一起使用。很遗憾,但我看不到任何解决方法。

    关于ios - 一般如何使用 iOS 14 单元格内容配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63075418/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com