增加数字并将其应用于tableView标头 - Swift

时间:2018-04-15 22:18:10

标签: ios swift xcode uitableview

我在视图控制器中创建了一个表视图。表格视图也有标题,当点击它们时,您可以打开和关闭每个部分。

我想在每个标题标题中添加一个不同的标题,第一个标题为01,第二个标题为02,依此类推。

我试过获取该部分的计数并将其作为标题。但是,它只是使每个标题都相同。我不知道如何从01增加并将其应用到每个部分?

任何帮助都会很棒,谢谢你。

我有一个二维数组,如下所示:

    var twoDimensionalArray = [
        shootDaysExpandableNames(isExpanded: true, shotNumber: ["Cell_001", "Cell_002", "Cell_015"]),
        shootDaysExpandableNames(isExpanded: true, shotNumber: ["Cell_004", "Cell_006", "Cell_008", "Cell_010", "Cell_014"]),
        shootDaysExpandableNames(isExpanded: true, shotNumber: ["Cell_0017", "Cell_009", "Cell_020", "Cell_024", "Cell_34"])
    ]

然后我的viewForHeaderInSection看起来像这样:

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

        let shotNumberInt = twoDimensionalArray[section].shotNumber.count
        let shootDayInt = twoDimensionalArray.count
        var shotNumberAmount = String(shotNumberInt) + (" Shots")
        var shootDay = String(shootDayInt)

        let line: UIView = {
            let line = UIView()
            line.backgroundColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0)
            line.translatesAutoresizingMaskIntoConstraints = false
            return line
        }()
        let openClose: UILabel = {
            let openClose = UILabel()
            openClose.text = "Open"
            openClose.textColor = UIColor(red:0.17, green:0.59, blue:0.30, alpha:1.0)
            openClose.font = UIFont(name: "Avenir-Medium", size: 12.0)
            openClose.translatesAutoresizingMaskIntoConstraints = false
            return openClose
        }()

        let shootDayNumber: UILabel = {
            let shootDayNumber = UILabel()
            shootDayNumber.text = shootDay
            shootDayNumber.textColor = UIColor(red:0.18, green:0.20, blue:0.21, alpha:1.0)
            shootDayNumber.font = UIFont(name: "AvenirNext-Bold", size: 40.0)
            shootDayNumber.translatesAutoresizingMaskIntoConstraints = false

            return shootDayNumber
        }()

        let shootDate: UILabel = {
            let shootDate = UILabel()
            shootDate.text = shotNumberAmount
            shootDate.textColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0)
            shootDate.font = UIFont(name: "Avenir-Medium", size: 12.0)
            shootDate.translatesAutoresizingMaskIntoConstraints = false
            return shootDate
        }()

        let button = UIButton(type: .system)
            button.backgroundColor = UIColor.white
            button.addSubview(line)
            button.addSubview(shootDayNumber)
            button.addSubview(openClose)
            button.addSubview(shootDate)
            button.addTarget(self, action: #selector(HandleOpenClose), for: .touchUpInside)
            button.tag = section

        // CONSTRAINTS

        shootDayNumber.centerYAnchor.constraint(equalTo: button.centerYAnchor, constant: 0).isActive = true
        shootDayNumber.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 17).isActive = true

        line.topAnchor.constraint(equalTo: button.topAnchor, constant: 10).isActive = true
        line.leftAnchor.constraint(equalTo: shootDayNumber.rightAnchor, constant: 5).isActive = true
        line.rightAnchor.constraint(equalTo: button.rightAnchor).isActive = true
        line.heightAnchor.constraint(equalToConstant: 1).isActive = true

        shootDate.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: -10).isActive = true
        shootDate.leftAnchor.constraint(equalTo: shootDayNumber.rightAnchor, constant: 5).isActive = true

        openClose.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: -10).isActive = true
        openClose.rightAnchor.constraint(equalTo: button.rightAnchor, constant: -17).isActive = true

        return button

    }

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

        let shotNumberInt = twoDimensionalArray[section].shotNumber.count
        let shootDayInt = twoDimensionalArray.count

        var shotNumberAmount = String(shotNumberInt) + (" Shots")
        var shootDay = String(shootDayInt)

        let line: UIView = {
            let line = UIView()
            line.backgroundColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0)
            line.translatesAutoresizingMaskIntoConstraints = false
            return line
        }()

        let shootDayNumber: UILabel = {
            let shootDayNumber = UILabel()
            shootDayNumber.text = shootDay
            shootDayNumber.textColor = UIColor(red:0.18, green:0.20, blue:0.21, alpha:1.0)
            shootDayNumber.font = UIFont(name: "AvenirNext-Bold", size: 40.0)
            shootDayNumber.translatesAutoresizingMaskIntoConstraints = false

            return shootDayNumber
        }()

        let shotCount: UILabel = {
            let shotCount = UILabel()
            shotCount.text = shotNumberAmount
            shotCount.textColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0)
            shotCount.font = UIFont(name: "Avenir-Medium", size: 12.0)
            shotCount.translatesAutoresizingMaskIntoConstraints = false
            return shotCount
        }()

        let button = UIButton(type: .system)
            button.backgroundColor = UIColor.white
            button.addSubview(line)
            button.addSubview(shootDayNumber)
            button.addSubview(shotCount)
            button.addTarget(self, action: #selector(HandleOpenClose), for: .touchUpInside)
            button.tag = section

        // CONSTRAINTS

        shootDayNumber.centerYAnchor.constraint(equalTo: button.centerYAnchor, constant: 0).isActive = true
        shootDayNumber.leftAnchor.constraint(equalTo: button.leftAnchor, constant: 17).isActive = true

        line.topAnchor.constraint(equalTo: button.topAnchor, constant: 10).isActive = true
        line.leftAnchor.constraint(equalTo: shootDayNumber.rightAnchor, constant: 5).isActive = true
        line.rightAnchor.constraint(equalTo: button.rightAnchor).isActive = true
        line.heightAnchor.constraint(equalToConstant: 1).isActive = true

        openClose.bottomAnchor.constraint(equalTo: button.bottomAnchor, constant: -10).isActive = true
        openClose.rightAnchor.constraint(equalTo: button.rightAnchor, constant: -17).isActive = true

        return button

仅供参考:我还在为每个标题添加一些其他UILabel,UIImages(这些标题在每个标题中都是相同的),但是我无法找到一种方法将它们添加到每个标题中而不创建和给出约束在viewForHeaderInSection函数中。不确定这是否是正确的方法呢?

1 个答案:

答案 0 :(得分:1)

这一行:

let shootDayInt = twoDimensionalArray.count

错了。它应该是

let shootDayInt = section + 1

(twoDimensionalArray中元素的数量不会改变,但传递到tableView(_:viewForHeaderInSection:) 的部分编号会发生变化。)