【发布时间】:2020-09-04 08:43:42
【问题描述】:
我正在构建一个 iOS 应用,在表格视图中包含一个集合视图。我有三行,每行都有一个集合视图。我计划为每一行的每个部分设置三个部分。例如行,一个应该在一个带有标题的单独部分中,对于第 2 行和第 3 行也是如此。 每当我创建三个部分时,我都会在所有三个部分中获得所有三行。我想有一个单独的部分,每行都有一个标题。
import UIKit
class StoreVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var CourseTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
CourseTableView.tableFooterView = UIView()
CourseTableView.delegate = self
CourseTableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0
{
return "Courses"
}
else if section == 1
{
return "Tests"
}
return "Bundles"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CourseRow
return cell
}
else if indexPath.row == 1
{
let cell = tableView.dequeueReusableCell(withIdentifier: "testcell", for: indexPath) as! TestRow
return cell
}
else if indexPath.row == 2
{
let cell = tableView.dequeueReusableCell(withIdentifier: "bundlecell", for: indexPath) as! BundleRow
return cell
}
return UITableViewCell()
}
}
【问题讨论】:
-
numberOfSections(in...) { return 3 }是 tableViews 的快速方法。然后在你的tableView(cellForRowAt..) { if section == 1 { //doSomething } else if section == 2 { //doSomething } else { //doSomething }最后设置numberOfRowsInSection to return 1
标签: ios swift uitableview uicollectionview uitableviewsectionheader