Swift collectionView.reloadsection未重新加载单元格
所以我使用合成布局来自动调整单元格大小。这就是我创建布局部分的方式。然后在UICollectionViewCompositionLayout(sectionProvider:)中返回它 这部分看起来像这样Swift collectionView.reloadsection未重新加载单元格,swift,uicollectionview,Swift,Uicollectionview,所以我使用合成布局来自动调整单元格大小。这就是我创建布局部分的方式。然后在UICollectionViewCompositionLayout(sectionProvider:)中返回它 这部分看起来像这样 class WhatsNewCell: UICollectionViewCell { static let reuseIdentifier = String(describing: WhatsNewCell.self) var delegate: WhatsNewCellDelega
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc private func handleMore() {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
delegate?.moreButtonClick()
}
}
var shouldShowFullDetails: Bool = false
func moreButtonClick() {
model?.shouldShowFullDetails = true
self.collectionView.reloadItems(at: [IndexPath(item: 0, section: 1)])
}
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
var model: Model? {
didSet {
configureCell()
}
}
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc func handleMore() {
delegate?.moreButtonClick()
}
private func configureCell() {
guard let model = model else { return }
if app.shouldShowFullReleaseNotes {
appUpdateLabel.numberOfLines = 0
} else {
appUpdateLabel.numberOfLines = 3
}
}
}
现在我有2个nib文件,每个文件都有自己的类。节标题只有一个标签和一个按钮。让我们忽略标题,因为问题在于WhatsNewCell。类文件看起来像这样
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc private func handleMore() {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
delegate?.moreButtonClick()
}
}
var shouldShowFullDetails: Bool = false
func moreButtonClick() {
model?.shouldShowFullDetails = true
self.collectionView.reloadItems(at: [IndexPath(item: 0, section: 1)])
}
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
var model: Model? {
didSet {
configureCell()
}
}
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc func handleMore() {
delegate?.moreButtonClick()
}
private func configureCell() {
guard let model = model else { return }
if app.shouldShowFullReleaseNotes {
appUpdateLabel.numberOfLines = 0
} else {
appUpdateLabel.numberOfLines = 3
}
}
}
好的,版本标签和更新标签在堆栈视图中。当用户单击按钮时,我会更改更新标签的行数。现在我创建了一个名为WhatsNewCellDelegate的委托,以便能够从控制器重新加载我的集合视图。这就是我在控制器中重新加载集合视图的方式。当我初始化一个单元格时,我也把单元格委托给了我的控制器
extension AppDetailCollectionVC: WhatsNewCellDelegate {
func moreButtonClick() {
//collectionView.collectionViewLayout.invalidateLayout()
collectionView.performBatchUpdates({
//collectionView.reloadData()
collectionView.reloadSections(IndexSet(integer: 2))
}, completion: nil)
}
}
现在,当我调用reloaddata时,它可以工作,但当我调用reloaddata时,它不工作。我必须按两次“更多”按钮,它才能处理重新加载部分。我也尝试使用invalidateLayout(),但它会使我的一些细胞消失。请看下面的屏幕截图
这是我使用invalidateLayout()单击“更多”按钮时的外观
所以我不知道发生了什么。我怎样才能在不必按两次“更多”按钮的情况下重新加载该部分。非常感谢您的帮助。尝试在主线程上运行它,而不使用
性能更新
DispatchQueue.main.async {
collectionView.reloadSections(IndexSet(integer: 2))
}
所以我让它工作了。我必须在我的模型中添加一个属性。像这样的
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc private func handleMore() {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
delegate?.moreButtonClick()
}
}
var shouldShowFullDetails: Bool = false
func moreButtonClick() {
model?.shouldShowFullDetails = true
self.collectionView.reloadItems(at: [IndexPath(item: 0, section: 1)])
}
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
var model: Model? {
didSet {
configureCell()
}
}
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc func handleMore() {
delegate?.moreButtonClick()
}
private func configureCell() {
guard let model = model else { return }
if app.shouldShowFullReleaseNotes {
appUpdateLabel.numberOfLines = 0
} else {
appUpdateLabel.numberOfLines = 3
}
}
}
然后我不得不这样做
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc private func handleMore() {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
delegate?.moreButtonClick()
}
}
var shouldShowFullDetails: Bool = false
func moreButtonClick() {
model?.shouldShowFullDetails = true
self.collectionView.reloadItems(at: [IndexPath(item: 0, section: 1)])
}
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
var model: Model? {
didSet {
configureCell()
}
}
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc func handleMore() {
delegate?.moreButtonClick()
}
private func configureCell() {
guard let model = model else { return }
if app.shouldShowFullReleaseNotes {
appUpdateLabel.numberOfLines = 0
} else {
appUpdateLabel.numberOfLines = 3
}
}
}
然后在我的手机档案里我不得不做这样的事情
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc private func handleMore() {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
delegate?.moreButtonClick()
}
}
var shouldShowFullDetails: Bool = false
func moreButtonClick() {
model?.shouldShowFullDetails = true
self.collectionView.reloadItems(at: [IndexPath(item: 0, section: 1)])
}
class WhatsNewCell: UICollectionViewCell {
static let reuseIdentifier = String(describing: WhatsNewCell.self)
var delegate: WhatsNewCellDelegate?
@IBOutlet weak var appVersionLabel: UILabel!
@IBOutlet weak var appUpdateLabel: UILabel!
@IBOutlet weak var moreButton: UIButton!
var model: Model? {
didSet {
configureCell()
}
}
override func awakeFromNib() {
super.awakeFromNib()
moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
@objc func handleMore() {
delegate?.moreButtonClick()
}
private func configureCell() {
guard let model = model else { return }
if app.shouldShowFullReleaseNotes {
appUpdateLabel.numberOfLines = 0
} else {
appUpdateLabel.numberOfLines = 3
}
}
}
如果你这么做会怎么样
extension AppDetailCollectionVC: WhatsNewCellDelegate {
func moreButtonClick() {
collectionView.collectionViewLayout.invalidateLayout()
}
}
我想当您第一次重新加载节时,collectionView会创建新的单元格和
self.appUpdateLabel.numberOfLines
采用初始值0
。
您可以在awakeFromNib
中添加断点,然后点击“更多”按钮进行检查
尝试加载项方法
@objc private func handleMore()
–self.invalidateIntrinsicContentSize()
正如Luis Ramirez所提到的,你们应该为电池使用一些外部配置
或者,如果在此集合视图中只有一个单元格的类型为WhatsNewCell
,则可以执行以下操作:
class WhatsNewCell: UICollectionViewCell {
private static var shouldShowDetails = false
override func awakeFromNib() {
super.awakeFromNib()
if Self.shouldShowDetails {
self.moreButton.setTitle("", for: .normal)
self.appUpdateLabel.numberOfLines = 0
} else {
self.moreButton.addTarget(self, action: #selector(handleMore), for: .touchUpInside)
}
}
@objc private func handleMore() {
Self.shouldShowDetails = true
....
}
}
我刚试过。我仍然需要点击“更多”按钮两次才能看到单元格重新加载。这会使我的一些单元格消失,我不知道为什么。请看屏幕截图,看看我使用invalidateLayout()时会发生什么。简单地说,您的单元格存储的信息在另一个单元格退出队列时不会持久化。此外,它不会重置。您必须在控制器中存储有关“更多”的信息,并始终在单元格中更新这些信息。