UITableViewCell中的UIListContentConfiguration和UIBackgroundConfiguration的基本用法? [英] Basic use of UIListContentConfiguration and UIBackgroundConfiguration in UITableViewCell?

查看:1428
本文介绍了UITableViewCell中的UIListContentConfiguration和UIBackgroundConfiguration的基本用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用新的iOS 14 UIListContentConfiguration和UIBackgroundConfiguration替换UITableViewCell配置,但是我什至看不到如何执行最基本的自定义.

I'm trying to experiment with replacing my UITableViewCell configuration with the new iOS 14 UIListContentConfiguration and UIBackgroundConfiguration, but I don't see how to perform even the most elementary customizations.

例如,我有一个测试单元,在我的数据源cellForRowAt:中,我像这样配置背景:

For example, I've got a test cell where, in my data source cellForRowAt:, I was configuring the background like this:

let v = UIImageView(image: UIImage(named:"linen.png"))
v.contentMode = .scaleToFill
cell.backgroundView = v

let v2 = UIView()
v2.backgroundColor = UIColor.blue.withAlphaComponent(0.2)
cell.selectedBackgroundView = v2

这个想法是,我们有一个图像作为单元格的背景,当用户点击该单元格以选择它时,我们用透明的蓝色色调覆盖它.选择单元格后,selectedBackgroundView位于backgroundView的前面.

The idea is that we have an image as the background of the cell, and when the user taps on the cell to select it, we overlay that with a transparent blue tint. The selectedBackgroundView sits in front of the backgroundView when the cell is selected.

好的,那么我该如何使用UIBackgroundConfiguration呢?我看到了如何配置背景视图:

Okay, so how do I do that with UIBackgroundConfiguration? I see how to configure the background view:

var back = UIBackgroundConfiguration.listPlainCell()
let v = UIImageView(image: UIImage(named:"linen.png"))
v.contentMode = .scaleToFill
back.customView = v
cell.backgroundConfiguration = back

但是当有选择时会发生什么呢?没有selectedCustomView,我在这里看不到针对不同州的不同背景的任何规定.

But what about what happens when there's a selection? There's no selectedCustomView, and I don't see any provision here for different backgrounds for different states.

推荐答案

以下似乎是您应该做的.在cellForRowAt:中,而不是配置背景视图,而是告诉单元格不要自动更新其背景:

The following seems to be what you're expected to do. In cellForRowAt:, instead of configuring the background view, tell the cell not to update its background automatically:

cell.automaticallyUpdatesBackgroundConfiguration = false

这意味着:我有一个自定义单元格子类,它覆盖了updateConfiguration(using:),我希望您在需要后台配置时调用该覆盖.

What that means is: I've got a custom cell subclass, it overrides updateConfiguration(using:), and I want you to call that override whenever you need the background configuration.

好吧,现在就这样做.在您的单元格子类中,覆盖updateConfiguration(using:).在该替代中,执行在cellForRowAt:中所做的所有操作,但是现在您可以考虑当前状态了.没有selectedCustomView,因此只需将选择色调覆盖在customView本身之上即可.

Okay, so now do that. In your cell subclass, override updateConfiguration(using:). In that override, do whatever you would have done in cellForRowAt:, but now you can take account of the current state. There is no selectedCustomView, so just do or don't overlay the selection tint on top of the customView itself:

class MyCell : UITableViewCell {
    override func updateConfiguration(using state: UICellConfigurationState) {
        var back = UIBackgroundConfiguration.listPlainCell().updated(for: state)
        let v = UIImageView(image: UIImage(named:"linen.png"))
        v.contentMode = .scaleToFill
        if state.isSelected || state.isHighlighted {
            let v2 = UIView()
            v2.backgroundColor = UIColor.blue.withAlphaComponent(0.2)
            v.addSubview(v2)
            v2.frame = v.bounds
            v2.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        }
        back.customView = v
        self.backgroundConfiguration = back
    }
}

那可以按预期工作.选择该单元格时,它具有蓝色.如果不是,那不是.

That works as expected. When the cell is selected, it has the blue tint; when it isn't, it doesn't.

乍一看,不得不为此专门创建一个单元格子类似乎很麻烦.但这实际上就是重点.这种新的基于配置的体系结构的全部原理是,从不进行数据源(cellForRowAt)的业务来详细配置单元格的 view 功能.从数据源的角度来看,配置应为 lightweight .让 cell 担心配置意味着的子视图.

At first blush, it may seem nutty to have to create a cell subclass just for this. But that is, in fact, the whole point. The entire philosophy of this new configuration-based architecture is that it was never the business of the data source (cellForRowAt) to configure the cell's view features in detail. The configuration should be lightweight from the point of view of the data source. Let the cell worry about what the configuration means in terms of its subviews.

这篇关于UITableViewCell中的UIListContentConfiguration和UIBackgroundConfiguration的基本用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆