在 swift 中更改 table 单元格 header

Altering table cell header on action in swift

我的分段 table 视图中有一个 header,其中包含一个按钮,点击时会展开其单元格,我在 header 中也有一个右向箭头,当点击 header 时,我想将该箭头图像更改为向下的箭头,如何更改该部分的 header?具体来说 arrowButton

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let header = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
        
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: tableView.frame.width - 30, height: 50))
        button.setTitle(tableDataSource[section][0].mod.modType!, for: .normal)
        button.titleLabel?.font = UIFont(name: "Helvetica neue", size: 20)
        button.addTarget(self, action: #selector(headerPressed(sender:)), for: .touchUpInside)
        button.titleLabel?.textAlignment = .left
        
        button.tag = section
        
        let arrowButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        arrowButton.setImage(UIImage(systemName: "chevron.right", compatibleWith: nil), for: .normal)
        header.addSubview(arrowButton)
        
        header.addSubview(button)
        header.backgroundColor = UIColor.green
        
        
        
        return header
    }

@objc func headerPressed(sender: UIButton){
        if(tableDataSource[sender.tag][0].clicked == false){
            tableDataSource[sender.tag][0].clicked = true
        } else {
            tableDataSource[sender.tag][0].clicked = false
        }
        let sections = IndexSet.init(integer: sender.tag)
        tableView.reloadSections(sections, with: .fade)
    }

你需要在里面改变这个viewForHeaderInSection

let name = tableDataSource[section][0].clicked ? "chevron.right" : "chevron.left"
arrowButton.setImage(UIImage(systemName:name, compatibleWith: nil), for: .normal)

调用此 tableView.reloadSections(sections, with: .fade) 将完成重新加载作业