gpt4 book ai didi

ios - 如何使用变量覆盖 UITableViewCell 的初始化?

转载 作者:行者123 更新时间:2023-11-29 05:14:29 25 4
gpt4 key购买 nike

所以,我有一个 UITableViewCell ,它有一个 Bool 变量,我需要用一个值初始化它。但我无法在类中指定它,因为它会重新加载并覆盖 didSelectRowAt 表方法中的传递值。

这是我的手机:

class DropDownTableViewCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!

var isOpenned: Bool

override func awakeFromNib() {
super.awakeFromNib()
self.selectionStyle = .none
}

func removesIcon() {
self.icon.image = nil
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}

func changeSelectedCell(_ selected: Bool) {
if selected {
self.icon.image = #imageLiteral(resourceName: "VectorUp")
self.bottomConstraint.constant = 0
} else {
self.icon.image = #imageLiteral(resourceName: "VectorDown")
self.bottomConstraint.constant = 16
}
}

这些是我的表格 View 方法

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let collapsed = selectedIndex == indexPath.section ? false : true

if indexPath.row == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? DropDownTableViewCell else {
fatalError("There should be a cell with \(identifier) identifier.")
}
cell.titleLabel.text = newModules[indexPath.section].sectionData
cell.icon.rotate(collapsed ? (2 * .pi) : .pi * 3)

if newModules[indexPath.section].section.isEmpty {
cell.removesIcon()
}
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: subIdentifier, for: indexPath) as? DropDownSubTableViewCell else {
fatalError("There should be a cell with \(identifier) identifier.")
}
cell.label.text = newModules[indexPath.section].section[indexPath.row - 1]
return cell
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if newModules[indexPath.section].section.isEmpty {
coordinator?.goToSubModule(subModules: parents[indexPath.section])
} else {
if indexPath.row == 0 {
if newModules[indexPath.section].isOpenned {
newModules[indexPath.section].isOpenned = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .automatic)
} else {
newModules[indexPath.section].isOpenned = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .automatic)
selectedIndex = indexPath.section
}
} else {
if parents.isEmpty {
coordinator?.goToSubModule(subModules: ((content?.sons[indexPath.row - 1])!))
} else {
coordinator?.goToSubModule(subModules: parents[indexPath.section].sons[indexPath.row - 1])
}
}
}
}

如您所见,tableView.reloadSections(sections, with: .automatic) 将其返回到之前的状态。所以我不能只说 cell.isOpenned = true 或类似的内容。

最佳答案

您需要最后打开的变量的索引路径为 int 而不是 bool 。按照下面的方法,它也适合您的情况。

multiple cells selected on scrolling [reuse cells problem]

关于ios - 如何使用变量覆盖 UITableViewCell 的初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59322558/

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