Uitableview 使用Swift 3展开/折叠TableView

Uitableview 使用Swift 3展开/折叠TableView,uitableview,swift3,tableviewcell,uitableviewsectionheader,Uitableview,Swift3,Tableviewcell,Uitableviewsectionheader,我确实在swift 3中折叠/展开了表格单元格。但是,我想在标题的底部添加一些填充。在我的截图中,标题1和标题2之间没有空格 另一个问题是如何将箭头图像向右移动 这是我的密码 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: 3

我确实在swift 3中折叠/展开了表格单元格。但是,我想在标题的底部添加一些填充。在我的截图中,标题1和标题2之间没有空格

另一个问题是如何将箭头图像向右移动

这是我的密码

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 28))
    var imageView = UIImageView()
    if (expandSections.contains(section)) {
        imageView = UIImageView.init(frame: CGRect(x: 7, y: 5, width: 25, height: 25))
        imageView.image = UIImage(named: "down_image")

    } else {
        imageView = UIImageView.init(frame: CGRect(x: 7, y: 5, width: 25, height: 25))
        imageView.image = UIImage(named: "up_image")
    }

    let headerTitle = UILabel.init(frame: CGRect(x: 38, y: 4, width: 250, height: 28))
    headerTitle.text = sectionData[section]
    headerTitle.textColor = UIColor.white

    let tappedSection = UIButton.init(frame: CGRect(x: 0, y: 0, width: headerView.frame.size.width, height: headerView.frame.size.height))
    tappedSection.addTarget(self, action: #selector(sectionTapped), for: .touchUpInside)
    tappedSection.tag = section

    headerView.addSubview(imageView)
    headerView.addSubview(headerTitle)
    headerView.addSubview(tappedSection)
    headerView.backgroundColor = UIColor.lightGray
    return headerView
}

试试这个,在这里我在headerView中添加了另一个视图。那么只有你可以在headerView中添加填充

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

        let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 28))

        let internalView = UIView.init(frame: CGRect(x: 0, y: 0, width: 300, height: 25))

        var imageView = UIImageView()

        if (expandSections.contains(section)) {

            imageView = UIImageView.init(frame: CGRect(x: internalView.frame.size.width - 10 , y: 5, width: 25, height: 25))
            imageView.image = UIImage(named: "down_image")

        } else {
            imageView = UIImageView.init(frame: CGRect(x: internalView.frame.size.width - 10 , y: 5, width: 25, height: 25))
            imageView.image = UIImage(named: "up_image")
        }

        let headerTitle = UILabel.init(frame: CGRect(x: 38, y: 4, width: 250, height: 28))
        headerTitle.text = sectionData[section]
        headerTitle.textColor = UIColor.white

        let tappedSection = UIButton.init(frame: CGRect(x: 0, y: 0, width: headerView.frame.size.width, height: headerView.frame.size.height))
        tappedSection.addTarget(self, action: #selector(sectionTapped), for: .touchUpInside)
        tappedSection.tag = section

        internalView.addSubview(imageView)
        internalView.addSubview(headerTitle)
        internalView.addSubview(tappedSection)

        headerView.addSubview(internalView)
        headerView.backgroundColor = UIColor.lightGray
        return headerView
    }

兄弟@Sasquatch,实际上我想完全分开这两个标题,看看背景颜色表。我怎么做?手机。headerView.backgroundColor=UIColor.tablebackgroundColor,那么当我为单元格声明类似时,它就可以工作了。让cell=tableView.dequeueReusableCell(标识符为“buy”,for:indexPath)为!植物细胞。我搞错了,你是不是犯了错误。如果进行customSell,则需要注册itBro@Sasquatch,错误为-->indexPath是未解析的标识符“indexPath”。func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?{} . 在这段代码中,viewForHeaderSection:Int。当我将Int更改为indexPath时。我也犯了错误。