如何自定义单元格的一部分

时间:2019-02-08 16:50:48

标签: ios swift uitableview

是否可以自定义单元格的一部分?可能最简单的方法是在情节提要中设计一个单元格,但我不知道如何在代码中实现它。

这是我到目前为止所得到的。它非常基础,是从youtube上的教程复制而来。因此,sectionData应该用自定义section / subCell的输入替换。

The upper cell should be the 'mainCell' and the cell below should be displayed after the mainCell is touched

import UIKit

struct cellData {
var opened = Bool()
var title = String()
var sectionData = [String]()
}

class ViewController: UITableViewController {

var tableViewData = [cellData]()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    tableViewData = [cellData(opened: false, title: "Title1", sectionData: ["Cell1","Cell2","Cell3"]),
                     cellData(opened: false, title: "Title2", sectionData: ["Cell1","Cell2","Cell3"]),
                     cellData(opened: false, title: "Title3", sectionData: ["Cell1","Cell2","Cell3"]),
                     cellData(opened: false, title: "Title4", sectionData: ["Cell1","Cell2","Cell3"])]
}

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableViewData[section].opened == true {
        return tableViewData[section].sectionData.count + 1
    } else {
        return 1
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let dataIndex = indexPath.row - 1

    if indexPath.row == 0  {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
        cell.textLabel?.text = tableViewData[indexPath.section].title
        return cell
    } else {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
        cell.textLabel?.text = tableViewData[indexPath.row].sectionData[dataIndex]
        return cell
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        if tableViewData[indexPath.section].opened == true {
            tableViewData[indexPath.section].opened = false
            let sections = IndexSet.init(integer: indexPath.section )
            tableView.reloadSections(sections, with: .none)
        } else {
            tableViewData[indexPath.section].opened = true
            let sections = IndexSet.init(integer: indexPath.section )
            tableView.reloadSections(sections, with: .none)
        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

https://www.appcoda.com/expandable-table-view/

您可以按照本教程进行操作。您可以使用以下代码重新加载要扩展的单元格。我在`didSelectRowAt中添加了。将expandCell变量设置为true,以在重新加载时更改单元格的高度。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

     // Expand View 
     self.expandCell = true 
     self.tableView.beginUpdates()
     self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
     self.tableView.endUpdates()
 }

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == expandRowIndex && self.expandCell {
          return 200
    }
    return UITableViewAutomaticDimension
 } 

但是您提出的问题与您想要实施的问题无关。无论如何,您的问题的答案是,您可以实现viewForHeaderInSectionviewForFooterInSection来自定义表格视图部分。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = "create your custom cell here or you can init from your nib"
        return cell
}

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

如果要在情节提要中执行此操作,只需将UITableViewCell拖放到表视图中,然后分配一些reuserIdentifier。在您的viewForHeaderInSection

中调用此tableview单元格
相关问题