ios - 如何自定义单元格的一部分
问题描述
有没有办法自定义一部分单元格?可能最简单的方法是在情节提要中设计一个单元格,但我不知道如何在我的代码中实现它。
这是我到目前为止得到的。它非常基础,是从 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)
}
}
}
}
解决方案
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
}
但是您提出的问题与您想要实施的时间无关。无论如何,您的问题的答案是,您可以实施viewForHeaderInSection
和viewForFooterInSection
自定义您的表格视图部分。
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
推荐阅读
- oracle - 错误唯一约束和触发oracle执行期间
- node.js - multer:带有音频和图像文件的请求发布的意外字段
- php - 如何在数据库中插入 DateTime,默认保存为 0000-00-00 00:00:00?
- alloy - 合金分析仪在 Mac OS X 10.14 上崩溃
- java - 生成的 PDF 中缺少图像
- r - 绘制由列定义的分组条形图
- javascript - 如何检索 html 'path' 元素属性值
- c++ - 迭代算法之前的高效内存分配
- reactjs - 如何更改状态并获取数据选择选项并在选项中显示数据
- spring-webflux - 如何将 Mono 流转换为 Flux