ios - How to override init an UITableViewCell with variable?
问题描述
So, I have an UITableViewCell
that has an Bool
variable and I need to initialize it with a value. But I can't attribute it in the class, because it gets reloaded and it override the passing value at didSelectRowAt
table method.
This is my cell:
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
}
}
And these are my tableview methods
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])
}
}
}
}
As you can see tableView.reloadSections(sections, with: .automatic
) returns it to theprevious state. So I can't just say cell.isOpenned = true
or something like that.
解决方案
您需要最后打开变量的 indexpath 作为 int 而不是 bool 。按照下面的方法,它在您的情况下也可以正常工作。
推荐阅读
- ios - 在 Swift 5 中追加数组
- node.js - 时刻无法正确解析字符串
- codemirror - CodeMirror getCursor 节点?
- javascript - 我该如何解决 --> File 是一个 CommonJS 模块;它可以转换为 ES6 模块
- moodle - 带有 SQL-UNION 的 Moodle 过滤器
- mysql - 触发器对每个阵营的所有元素求和
- css - 如何获取使用 ExCSS 解析的 CSS 的十六进制代码
- video - FFMPEG 从视频中删除音频使视频变短
- ios - 如果它与之前的值相似,则忽略最后一个值,运算符组合框架
- protractor - 量角器:如何编写定位器来选择所需的div并单击它上面的按钮?