UITableViewCell中UIListContentConfiguration和UIBackgroundConfiguration的基本使用?
Basic use of UIListContentConfiguration and UIBackgroundConfiguration in UITableViewCell?
我正在尝试用新的 iOS14 UIListContentConfiguration 和 UIBackgroundConfiguration 替换我的 UITableViewCell 配置,但我不知道如何执行最基本的自定义。
例如,我有一个测试单元,在我的数据源 cellForRowAt:
中,我是这样配置背景的:
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
我们的想法是,我们有一个图像作为单元格的背景,当用户点击单元格以 select 它时,我们用透明的蓝色覆盖它。 selectedBackgroundView
在 selected 单元格时位于 backgroundView
前面。
好的,那么我如何使用 UIBackgroundConfiguration 来做到这一点?我看看如何配置背景视图:
var back = UIBackgroundConfiguration.listPlainCell()
let v = UIImageView(image: UIImage(named:"linen.png"))
v.contentMode = .scaleToFill
back.customView = v
cell.backgroundConfiguration = back
但是当有 selection 时会发生什么?没有selectedCustomView
,我在这里也没有看到针对不同州的不同背景的任何规定。
以下似乎是您需要执行的操作。在 cellForRowAt:
中,不配置背景视图,而是告诉单元格不要自动更新其背景:
cell.automaticallyUpdatesBackgroundConfiguration = false
这意味着:我有一个自定义单元格子类,它覆盖了 updateConfiguration(using:)
,我希望您在需要后台配置时调用该覆盖。
好的,现在开始吧。在您的单元格子类中,覆盖 updateConfiguration(using:)
。在该覆盖中,执行您在 cellForRowAt:
中执行的任何操作,但现在您可以考虑当前状态。没有 selectedCustomView
,所以只需在 customView
本身之上覆盖或不覆盖选择色调即可:
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
}
}
按预期工作。选择单元格时,它具有蓝色调;如果不是,那就不是。
乍一看,为此创建一个单元格子类似乎很疯狂。但事实上,这就是重点。这种新的基于配置的架构的整个理念是,从不数据源 (cellForRowAt
) 的业务来配置单元格的 view 功能详细。从数据源的角度来看,配置应该是轻量级。让 cell 担心配置 意味着 就其子视图而言。
我正在尝试用新的 iOS14 UIListContentConfiguration 和 UIBackgroundConfiguration 替换我的 UITableViewCell 配置,但我不知道如何执行最基本的自定义。
例如,我有一个测试单元,在我的数据源 cellForRowAt:
中,我是这样配置背景的:
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
我们的想法是,我们有一个图像作为单元格的背景,当用户点击单元格以 select 它时,我们用透明的蓝色覆盖它。 selectedBackgroundView
在 selected 单元格时位于 backgroundView
前面。
好的,那么我如何使用 UIBackgroundConfiguration 来做到这一点?我看看如何配置背景视图:
var back = UIBackgroundConfiguration.listPlainCell()
let v = UIImageView(image: UIImage(named:"linen.png"))
v.contentMode = .scaleToFill
back.customView = v
cell.backgroundConfiguration = back
但是当有 selection 时会发生什么?没有selectedCustomView
,我在这里也没有看到针对不同州的不同背景的任何规定。
以下似乎是您需要执行的操作。在 cellForRowAt:
中,不配置背景视图,而是告诉单元格不要自动更新其背景:
cell.automaticallyUpdatesBackgroundConfiguration = false
这意味着:我有一个自定义单元格子类,它覆盖了 updateConfiguration(using:)
,我希望您在需要后台配置时调用该覆盖。
好的,现在开始吧。在您的单元格子类中,覆盖 updateConfiguration(using:)
。在该覆盖中,执行您在 cellForRowAt:
中执行的任何操作,但现在您可以考虑当前状态。没有 selectedCustomView
,所以只需在 customView
本身之上覆盖或不覆盖选择色调即可:
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
}
}
按预期工作。选择单元格时,它具有蓝色调;如果不是,那就不是。
乍一看,为此创建一个单元格子类似乎很疯狂。但事实上,这就是重点。这种新的基于配置的架构的整个理念是,从不数据源 (cellForRowAt
) 的业务来配置单元格的 view 功能详细。从数据源的角度来看,配置应该是轻量级。让 cell 担心配置 意味着 就其子视图而言。