在可展开和可折叠表格视图部分中滚动时,如何修复UIButton更改状态

时间:2018-12-20 16:03:06

标签: ios swift uitableview swift4 uitableviewsectionheader

在一周的几天中,我的tableview单元格中都有一个可扩展和可折叠的部分。单击“打开”按钮以展开该部分时,该按钮会将状态更改为关闭,然后可以再次单击以使其折叠。但是,如果您打开多个部分并开始在表格视图中滚动,则这些部分将关闭并打开,但是一旦所有部分都已从滚动中重新打开,则按钮状态不会更改回关闭状态。

我正在做一些研究,看来状态还没有得到保存。有什么方法可以使用viewForHeaderInSection中的dequeuereusableheaderfooterview()保存这些按钮的状态吗?还是必须在cellForRowAt函数中完成?

var barDeals: [ExpandableDealsStruct] = []


@IBOutlet weak var daysTable: UITableView!

override func viewDidLoad()
{
    super.viewDidLoad()

    daysTable.dataSource = self
    daysTable.delegate = self

}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{

    let view = UIView()
    view.backgroundColor = .black

    let headerLabel = UILabel()
    let button = UIButton(type: .system)

    let headerList = DaysService.instance.getHeader()
    headerLabel.textColor = .white
    headerLabel.font = UIFont.boldSystemFont(ofSize: 20)
    headerLabel.text = headerList[section]

    button.setTitle("Open", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
    button.addTarget(self, action: #selector(handleOpenClose), for: .touchUpInside)
    button.tag = section

    headerLabel.frame = CGRect(x: 5, y: 7.5, width: 150, height: 25)
    button.frame = CGRect(x: 150, y: 7.5, width: 150, height: 25)


    view.addSubview(headerLabel)
    view.addSubview(button)

    return view
}

@objc func handleOpenClose(button: UIButton)
{
    let section = button.tag

    self.daysTable.beginUpdates()
    var indexPaths = [IndexPath]()

    for deals in self.barDeals[section].deals.indices
    {
        let indexPath = IndexPath(row: deals, section: section)
        indexPaths.append(indexPath)
    }

    let isExpanded = self.barDeals[section].isExpanded
    self.barDeals[section].isExpanded = !isExpanded

    button.setTitle(!isExpanded ? "Close" : "Open", for: .normal)

    if isExpanded
    {
        daysTable.deleteRows(at: indexPaths, with: .fade)
    }
    else
    {
        daysTable.insertRows(at: indexPaths, with: .fade)
    }

    self.daysTable.endUpdates()

}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat
{
    return 1
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
    return 40
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

    if self.barDeals[section].isExpanded == false
    {
        return 0
    }
    return self.barDeals[section].deals.count
}

func numberOfSections(in tableView: UITableView) -> Int
{
    return self.barDeals.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "DaysCell", for: indexPath)
    let deals = self.barDeals[indexPath.section].deals[indexPath.row]
    cell.textLabel?.text = deals
    return cell
}


}

0 个答案:

没有答案