如何将表格视图划分为多个部分并提供标题

时间:2018-11-28 14:40:07

标签: swift uitableview

我的应用程序中有一个tableview。 我想将单元格划分为多个部分并提供标题。

在下面附上图片(动物,鸟类的头条新闻):

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 :(得分:3)

 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"
    }

答案 1 :(得分:2)

让我们先清理代码:

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
}

您还可以使用自定义可重用标头,该标头将以与单元格出队相同的方式进行注册和出队。

答案 2 :(得分:0)

现在我明白了。您误解了这些方法的工作方式。每个部分的每个单元格都会调用它们。

因此,如果您在numberOfSections中返回4,则方法numberOfRowsInSection将被调用4次,而sectionIndex(在最新版本中现在称为section)将具有当前部分(在我们的示例中为0到3)。

因此,如果您想将第二部分称为“ Birds”,而让所有其他部分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
}

请记住Sulthan的建议,为您的部分创建一个枚举。