无法在tableView中插入新行

时间:2019-01-26 10:17:23

标签: ios swift uitableview

我知道问题已经解决了很多次,但是没有一种方法可以帮助我。

我想在tableView中插入新行。我有三个部分,在第二和第三部分中,我有UIButton可以将新行推断为所需部分,但是在控制台中收到错误消息。可能是什么问题?

我在插入新行时遇到的错误-attempt to insert row 2 into section 1, but there are only 2 rows in section 1 after the update

我的代码:

class SettingsScheduleAndPricesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var saveBarButtonItem: UIBarButtonItem!
    @IBOutlet weak var backBarButtonItem: UIBarButtonItem!

    let sectionTitle = ["Day of week", "Second section", "Third section"]
    var secondRowText = ["First row", "Second row"]
    var thirdRowText = ["Row one", "Row two", "Row three"]

    override func viewDidLoad() {
        super.viewDidLoad()    
    }
    // MARK: - TableView
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionTitle.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0: return 1
        case 1: return 2
        default: return 3
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let collectionCell = tableView.dequeueReusableCell(withIdentifier: "collectionCell", for: indexPath) as! SettingsCollectionViewCell
            return collectionCell
        } else if indexPath.section == 1 {
            let hourlyRateCell = tableView.dequeueReusableCell(withIdentifier: "hourlyRateCell", for: indexPath) as! SettingsHourlyRateCell
            hourlyRateCell.textLabel?.text = secondRowText[indexPath.row]
            return hourlyRateCell
        } else {
            let datePriceCell = tableView.dequeueReusableCell(withIdentifier: "datePriceCell", for: indexPath) as! SettingsDatePriceCell
            datePriceCell.textLabel?.text = thirdRowText[indexPath.row]
            return datePriceCell
        }
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.sectionTitle[section]
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let frame: CGRect = tableView.frame
        if section == 0 {
            let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            return headerView
        } else if section == 1 {
            let addButton: UIButton = UIButton(frame: CGRect(x: frame.size.width - 50, y: 0, width: 50, height: 30))
            addButton.backgroundColor = UIColor.clear
            addButton.setTitleColor(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), for: .normal)
            addButton.setTitle("Add", for: .normal)
            addButton.addTarget(self, action: #selector(SettingsScheduleAndPricesViewController.addHourlyRate(sender:)), for: .touchUpInside)
            let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            headerView.addSubview(addButton)
            return headerView
        } else {
            let addButton: UIButton = UIButton(frame: CGRect(x: frame.size.width - 50, y: 0, width: 50, height: 30))
            addButton.backgroundColor = UIColor.clear
            addButton.setTitleColor(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), for: .normal)
            addButton.setTitle("Add", for: .normal)
            addButton.addTarget(self, action: #selector(SettingsScheduleAndPricesViewController.addDatePrice(sender:)), for: .touchUpInside)
            let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            headerView.addSubview(addButton)
            return headerView
        }
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }

    @objc func addHourlyRate(sender: UIButton) {
        let newRow: String = "Third row"
        secondRowText.append(newRow)
        let indexPath = IndexPath(row: secondRowText.count - 1, section: 1)
        tableView.beginUpdates()
        tableView.insertRows(at: [indexPath], with: .automatic)
        tableView.endUpdates()
    }

    @objc func addDatePrice(sender: UIButton) {
        let newRow: String = "Row four"
        thirdRowText.append(newRow)
        let indexPath = IndexPath(row: thirdRowText.count - 1, section: 2)
        tableView.beginUpdates()
        tableView.insertRows(at: [indexPath], with: .automatic)
        tableView.endUpdates()
    }
}
我在delegate中添加的

dataSourcestoryboard

请不要因为重复这个问题而责骂,所有发现的答案都对我没有帮助

1 个答案:

答案 0 :(得分:2)

强烈建议不要硬编码numberOfSectionsnumberOfRowsInSection的返回值。这导致了错误。

另一方面,强烈建议您使用自定义结构作为数据模型。这样效率更高,并且可以避免那些错误。

struct Section {
    let title : String
    var rows : [String]
}

class SettingsScheduleAndPricesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var saveBarButtonItem: UIBarButtonItem!
    @IBOutlet weak var backBarButtonItem: UIBarButtonItem!

    var sections = [Section]()

    override func viewDidLoad() {
        super.viewDidLoad()
        sections = [Section(title: "Day of week", rows: []),
                    Section(title: "Second section", rows: ["First row", "Second row"]),
                    Section(title: "Day of week", rows: ["Row one", "Row two", "Row three"])]
    }

    // MARK: - TableView
    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].rows.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section].title
    }

/*  
    `cellForRowAt`, `viewForHeaderInSection` and `heightForHeaderInSection` omitted
*/

    @objc func addHourlyRate(sender: UIButton) {
        let newRow = "Third row"
        append(row: newRow, in: 1)
    }

    @objc func addDatePrice(sender: UIButton) {
        let newRow = "Row four"
        append(row: newRow, in: 2)
    }

    func append(row : String, in section: Int) {
        let insertionIndex = sections[section].rows.count
        sections[section].rows.append(row)
        let indexPath = IndexPath(row: insertionIndex, section: section)
        tableView.insertRows(at: [indexPath], with: .automatic)
    }
}