Как добавить несколько строк в разных разделах в табличном представлении в Swift - PullRequest
0 голосов
/ 21 мая 2019

У меня есть вид таблицы в этой ячейке. Я создал вид заголовка в этом заголовке. У меня есть метка и кнопка, я получаю значения метки из массива, и этот массив подсчитывает количество секций, что работает нормально. но когда я нажимаю на кнопку, мне нужно расширить ячейку, и в этой расширенной ячейке мне нужно добавить метку, но пока я нажимаю кнопку, она говорит

ошибка: индекс вне диапазона

в numberOfRowsInSection и не расширяется.

вот мой код:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DataSendingDelegateProtocol, FieldsDelegateProtocol {
var shouldCellBeExpanded:Bool = false
var indexOfExpendedCell:NSInteger = -1
// let kHeaderSectionTag: Int = 6900;

@IBOutlet weak var tableView: UITableView!
var sectionArray = [String]()
var sectionItemsArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    //self.tableView!.tableFooterView = UIView()
}

override func viewWillAppear(_ animated: Bool)
{
    super.viewWillAppear(animated)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func sendDataToTableRow(myData: String) {
    self.sectionItemsArray.append(myData)
    tableView.reloadData()
   // print(iteamsArray)
}

func sendDataToSectionLabel(myData: String) {
    self.sectionArray.append(myData)
    tableView.reloadData()
    print(sectionArray)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "segue") {
        let vc = segue.destination as! PopViewController
        vc.delegate = self
    }
    else if (segue.identifier == "fieldsSegue") {
        let vc = segue.destination as! FieldsViewController
        vc.delegate = self
    }
}
@objc func expandButnClicked(sender:UIButton){

    print("expand button tapped")

    shouldCellBeExpanded = !shouldCellBeExpanded
    indexOfExpendedCell = sender.tag

    if shouldCellBeExpanded {

        self.tableView.beginUpdates()
        self.tableView.endUpdates()
    }
    else {
        self.tableView.beginUpdates()
        self.tableView.endUpdates()
    }
}

@IBAction func addBtn(_ sender: Any) {
}

// MARK: - Tableview Methods
func numberOfSections(in tableView: UITableView) -> Int {

    return sectionArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if (self.indexOfExpendedCell == section) {
        let arrayOfItems = self.sectionItemsArray[section]
        return arrayOfItems.count
    } else {
        return 0
    }
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if (self.sectionArray.count != 0) {
        return self.sectionArray[section] as? String
    }
    return ""
}

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

/*func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
    return 0;
}*/

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.contentView.backgroundColor = UIColor.colorWithHexString(hexStr: "#0075d4")
    header.textLabel?.textColor = UIColor.white

    let button = UIButton(frame: CGRect(x: 380, y: 10, width: 15, height: 15))
    button.backgroundColor = UIColor.green
    button.tag = section
    button.addTarget(self, action: #selector(expandButnClicked), for: .touchUpInside)

    header.addSubview(button)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell {
        return 200
    }
    else {
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell

    cell.textLab?.textColor = UIColor.black
    cell.textLab.text = sectionItemsArray[indexPath.row]
    cell.backgroundColor = UIColor.black
    return cell
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
}

Пожалуйста, помогите мне в коде.

Ответы [ 2 ]

0 голосов
/ 21 мая 2019

Ваша проблема может быть определена двумя способами, которые вы реализовали:

func numberOfSections(in tableView: UITableView) -> Int {

    return sectionArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if (self.indexOfExpendedCell == section) {
        let arrayOfItems = self.sectionItemsArray[section]
        return arrayOfItems.count
    } else {
        return 0
    }
}

В первом вы объясняете, сколько разделов у вас есть, для которых вы использовали sectionArray, а затем во втором вы используете другой массив sectionItemsArray. Ваш сбой наиболее вероятен в случае sectionItemsArray[sectionArray.count-1], который завершится неудачей, если sectionItemsArray содержит меньше элементов, чем sectionArray.

Вероятно, вы должны иметь это согласованно и использовать только один массив. В вашем случае имеет смысл использовать массив массивов для представления разделов и строк. Но в целом может быть лучше использовать пользовательские структуры. Рассмотрим что-то вроде следующего:

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    class Section {
        class Row {
            let name: String
            var shown: Bool = true
            init(name: String) { self.name = name }
        }
        let name: String
        var shown: Bool = true
        var rows: [Row]

        init(name: String, rows: [Row] = [Row]()) { self.name = name; self.rows = rows }
    }

    var sections: [Section]!

    override func viewDidLoad() {
        super.viewDidLoad()
        sections = [
            Section(name: "Empty"),
            Section(name: "Second section", rows: [.init(name: "row 1"), .init(name: "row 2")]),
            Section(name: "Third section", rows: [.init(name: "row 1"), .init(name: "row 2")])
        ]
    }


    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.filter { $0.shown }.count
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections.filter { $0.shown }[section].name
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let name: String = sections.filter { $0.shown }[indexPath.section].rows.filter { $0.shown }[indexPath.row].name
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = name
        return cell
    }

}
0 голосов
/ 21 мая 2019

Если раздел расширен, вернуть счетчик массива sectionItemsArray в методе numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (self.indexOfExpendedCell == section) {
        return self.sectionItemsArray.count
    } else {
        return 0
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...