【问题标题】:TableView Expand/Collapse with Swift 3TableView 使用 Swift 3 展开/折叠
【发布时间】:2017-04-04 08:32:29
【问题描述】:

我确实在 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
}

【问题讨论】:

    标签: uitableview swift3 tableviewcell uitableviewsectionheader


    【解决方案1】:

    试试这个,在这里我在 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(withIdentifier: "buy", for: indexPath) as!计划表单元格。我遇到了错误。
    • 你做了customCell吗?如果你做customSell,你需要注册它
    • 兄弟@Sasquatch错误是--> indexPath是未解析的标识符'indexPath'。 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {} 。在此代码中,viewForHeaderInSection 部分:Int。当我将 Int 更改为 indexPath。我也有错误。
    猜你喜欢
    • 1970-01-01
    • 2013-07-14
    • 2016-08-25
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2022-01-26
    相关资源
    最近更新 更多