Как разделить табличное представление на разделы и дать заголовки - PullRequest
0 голосов
/ 28 ноября 2018

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

Ниже я прилагаю картинку (заголовки животных, птиц):

enter image description here

Как мне это сделать в динамических ячейках прототипа?

Данные моего раздела:

   var sectionsData = [
       "header", 
       "description", 
       "diagnoses", 
       "perscription", 
       "notes", 
       "addFaxHeadline",
       "addFax", 
       "addEmailHeadline",
       "addEmails",
       "givePermissionHeadline", 
       "select answer"
   ]

Вот моя ячейка в строке:

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

        print ("indexPath: ", indexPath)
        print ("indexPath: ", indexPath[0])
        print ("-------")

        if (sectionsData[indexPath[0]] == "header") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "description") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "headerInfoCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "diagnoses") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "diagnosisCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "perscription") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "perscriptionCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "notes") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "notesCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "addFaxHeadline") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "addFaxCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "addFax") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "emailNameCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "addEmailHeadline") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "addEmailCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "addEmails") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "emailNameCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "givePermissionHeadline") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "permisionCell", for: indexPath)

            return cell

        } else if (sectionsData[indexPath[0]] == "select answer") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "selectAnswerCell", for: indexPath)

            return cell   

        }

        let cell = tableView.dequeueReusableCell(withIdentifier: "addFaxCell", for: indexPath)

        // <<<< ???

        return cell

    }

и мои числав строке для каждой секции:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows

        if (sectionsData[section] == "header") {
            print ("returning 1 ?")
            return 1
        } else if (sectionsData[section] == "description") {
            return 1

        } else if (sectionsData[section] == "diagnoses") {

            //return visitSummary.diagnoses.count
            return 2

        } else if (sectionsData[section] == "perscription") {
            //return visitSummary.prescriptions.count
            return 2

        } else if (sectionsData[section] == "notes") {
            return 1

        } else if (sectionsData[section] == "addFaxHeadline") {
            return 1

        } else if (sectionsData[section] == "addFax") {
            return faxAdded.count

        } else if (sectionsData[section] == "addEmailHeadline") {
            return 1

        } else if (sectionsData[section] == "addEmails") {
            return emailsAdded.count

        } else if (sectionsData[section] == "givePermissionHeadline") {
            return 1

        } else if (sectionsData[section] == "select answer") {
            return 1

        } else {
            return 0
        }

        return 0
    }

и мое количество секций:

 override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        print ("sectionsData.count: ", sectionsData.count)
        return sectionsData.count
    }

Все учебники, которые я нахожу, предназначены для одной секции, и у меня есть очень много секций, которые нужно показать.

Как бы я разделил это и дал им заголовки?

Ответы [ 3 ]

0 голосов
/ 28 ноября 2018
 var sectionsData = [
           "header", 
           "description", 
           "diagnoses", 
           "prescription", 
           "notes", 
           "addFaxHeadline",
           "addFax", 
           "addEmailHeadline",
           "addEmails",
           "givePermissionHeadline", 
           "select answer"
       ] // Your Array

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? //*Data Source method for header title*
    {
        // Safe cast
        if let sectionArray = sectionsData as? [String]
        {
            return sectionArray[section]
        }

        // A fail safe
        return "No Header"
    }
0 голосов
/ 28 ноября 2018

Теперь я вижу.Вы неправильно понимаете, как работают эти методы.Они вызываются для каждой ячейки в каждом разделе.

Итак, если вы вернете 4 в numberOfSections, метод numberOfRowsInSection будет вызван 4 раза, а sectionIndex (теперь он вызывается section в последнемверсии) будет иметь текущий раздел (от 0 до 3 в нашем примере).

Поэтому, если вы хотите назвать второй раздел «Птицы» и разрешить всем остальным nil, ваш код будет выглядеть следующим образом:

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 1 {
        return "Birds"
    }
    // Otherwise
    return nil
}

То же самое для количества строк в каждом разделе:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        // 1 row for the first section
        return 1
    } else if section == 1 {
        // 3 rows for the second section
        return 3
    }
    // 2 rows for every other section
    return 2
}

И, наконец, какая ячейка будет использоваться в каждом indexPath:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Figure out which row and section you are talking about
    let row = indexPath.row
    let section = indexPath.section

    // Now you know where you are in the TableView, create the corresponding cell according to your project
    // Start by dequeueing a custom cell using the identifier and forcing the cell to become your custom cell
    let cell = tableView.dequeueReusableCell(withIdentifier: customCellID) as! CustomTableViewCell

    // Do aditional setup to this cell
    cell.textLabel?.text = "This is cell \(row) in section \(section)"

    // Return the cell when you are ready
    return cell
}

Помните предложение Султана создать перечисление для ваших разделов.

0 голосов
/ 28 ноября 2018

Давайте сначала очистим ваш код:

enum Section: Int, CaseIterable {
   case header = 0, description, diagnoses, prescription, notes, addFaxHeadline,  addFax, addEmailHeadline, addEmails, givePermissionHeadline, selectAnswer
}

var sectionData: [Section] = [
   .header,
   .description,
   .diagnoses
   ...
]

override func numberOfSections(in tableView: UITableView) -> Int {
    return sectionsData.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection sectionIndex: Int) -> Int {
    let section = sectionData[sectionIndex]

    switch section {
       case .header:
          return 1
       case .description:
          return 1
       // ... 
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let section = sectionData[sectionIndex]

    switch section {
       case .header:
          let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath)

          return cell
      // ... etc
    }
}

Теперь мы можем вернуть заголовок раздела, используя String:

override func tableView(_ tableView: UITableView, titleForHeaderInSection sectionIndex: Int) -> String? {
    let section = sectionData[sectionIndex]

    switch section {
       case .header:
          return "Header title"
       case .description:
          return "Description title"
       default:
          return nil
    }
}

Другой вариант - использовать tableView (: viewForHeaderInSection:) и tableView (: heightForHeaderInSection:) :

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

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    let label = UILabel()
    view.addSubview(label)
    // setup constraints accordingly
    // setup title of the label
    return view
}

Вы также можете использовать настраиваемый многоразовый заголовок, который будет зарегистрировани убираются так же, как и клетки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...