首页 > 解决方案 > 如何自定义单元格的一部分

问题描述

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

这是我到目前为止得到的。它非常基础,是从 youtube 上的教程中复制而来的。所以 sectionData 应该替换为自定义部分/子单元的输入。

上面的单元格应该是'mainCell',下面的单元格应该在mainCell被触摸后显示

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

}
}

标签: iosswiftuitableview

解决方案


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 拖放到您的 tableview 中,分配一些 reuserIdentifier。在你的调用这个 tableview 单元格viewForHeaderInSection


推荐阅读