首页 > 解决方案 > 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.

标签: iosswiftuitableview

解决方案


您需要最后打开变量的 indexpath 作为 int 而不是 bool 。按照下面的方法,它在您的情况下也可以正常工作。

滚动时选择了多个单元格[重用单元格问题]


推荐阅读