【问题标题】:How to add another section (button) after some data in table view swift?如何在表格视图中的某些数据之后快速添加另一个部分(按钮)?
【发布时间】:2021-01-08 19:03:25
【问题描述】:

我正在尝试根据用户输入创建多个部分。 我能够做到这一点,但下一步是在该部分的末尾创建按钮。 但是,我们不知道最后一部分是什么,因为它取决于用户的请求。 它看起来像这样:

现在,我为按钮创建了另一个单元格,如下所示:

到目前为止,这是我的代码:

var numberOfSection: Int = 0 {
    didSet {
        tableView.reloadData()
    }
}

override func numberOfSections(in tableView: UITableView) -> Int {
    if numberOfSection == 0 {
        return 2
    }
    else if numberOfSection <= numberOfSection {
        return numberOfSection + 1
    }
    return numberOfSection + 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
    }
    else if section <= numberOfSection + 1 {
        for _ in 0...numberOfSection {
            return testeSpec.count
        }
    }
    else {
        return 1
    }
    return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let tableSection = indexPath.section
    if tableSection == 0 {
        let cells = tableView.dequeueReusableCell(withIdentifier: "serverNumbCell", for: indexPath) as! CellOutlet
        let txtFieldServerNumber = cells.txtFieldServerNumb
        cells.lblSectionNumb.text = "Number of section:"
        txtFieldServerNumber?.addTarget(self, action: #selector(sectionTxtFieldDidChange), for: .editingChanged)
        return cells
    }
    
    else if tableSection <= numberOfSection + 1 {
        for _ in 0...numberOfSection {
            let cells = tableView.dequeueReusableCell(withIdentifier: "serverSpecCell", for: indexPath) as! CellOutlet
            let specWS = testSpec[indexPath.row]
            cells.labels.text = specWS.spec
            return cells
        }
    }
    else {
        let cells = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath) as! CellOutlet
        cells.btnResult.setTitle("OK", for: .normal)
        return cells
    }
    return CellOutlet()
}

我有另一个班级与所有出口挂钩。 在所有这些未知数量的部分之后,如何在一个部分中创建另一个按钮?

提前谢谢你

【问题讨论】:

    标签: ios swift uitableview tableview sections


    【解决方案1】:

    在 ViewController 类中

    class ViewController: UIViewController {
        
        var numberOfSections: Int = 0
        @IBOutlet weak var tableView: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.dataSource = self
            tableView.delegate = self
        }
    }
    extension ViewController: UITableViewDelegate, UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return numberOfSections + 1
        }
        
        func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            "section"
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if section == 0 {
                return 1
            } else if numberOfSections > section {
                return 2
            } else {
                return 1
            }
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            if indexPath.section == 0 {
                guard let cell = tableView.dequeueReusableCell(withIdentifier: "ServerSpecCell") as? ServerSpecCell else {return UITableViewCell()}
                
                cell.titleLabel.text = "Number of section"
                cell.numberOfSectionAction = { userInput in
                    self.numberOfSections = userInput
                    self.tableView.reloadData()
                }
                return cell
            }
            
            if numberOfSections > indexPath.section {
                guard let cell = tableView.dequeueReusableCell(withIdentifier: "ServerSpecCell") as? ServerSpecCell else {return UITableViewCell()}
                return cell
            }
            
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell") as? ButtonCell else {return UITableViewCell()}
            return cell
        }
    }
    
    
    class ButtonCell: UITableViewCell {
        
        
        override class func awakeFromNib() {
            super.awakeFromNib()
            
        }
    }
    

    在 UITableViewCell 子类中

    class ServerSpecCell: UITableViewCell, UITextFieldDelegate {
    
        @IBOutlet weak var titleLabel: UILabel!
        @IBOutlet weak var serverTextField: UITextField!
        
        var numberOfSectionAction: ((Int)->Void)?
        
        override func awakeFromNib() {
            super.awakeFromNib()
            serverTextField.delegate = self
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
        
        func textFieldDidChangeSelection(_ textField: UITextField) {
            guard let sections = Int(textField.text ?? "0") else { return }
            numberOfSectionAction?(sections+1)
        }
    
    }
    

    【讨论】:

    • 我试过了,但是还是不行,numberOfSectionsnumberOfRowsInSection有什么需要改的?
    • 以 userInputCount+1 的形式返回 numberOfSections。在numberOfRowsInSection 中,如果 indexPath.section == userInputCount+1 则返回 1,否则返回 1。
    • 不知何故我不需要子类..这很奇怪,但感谢您的帮助,感谢!
    猜你喜欢
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多