带有自定义单元格的 UITableView 中的部分
Sections in UITableView with Custom Cells
到目前为止我有以下代码。
var someData = [SomeData]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2
let someData = [indexPath.row]
//Set up labels etc.
return cell!
}
}
我需要 Cell1,它是一个静态单元格,并且将始终保持在 indexPath 0 以位于名为 "Section1" 的部分中,并且所有 Cell2 都位于名为 "Section2"[ 的部分中=12=]
其他数据源和委托方法;
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else {
return someData.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Section1" }
else {
return "Section2"
}
}
这 returns 我需要第一部分所需的一切,但是,当涉及到第二部分时(由于 cellForRowAtIndex 中某处的代码),第 2 部分在 indexPath 0 处包含 Cell2。
非常感谢任何帮助。
根本原因:
在 cellForRowAtIndexPath
中检查 indexPath.section
而不是 indexPath.row
修复:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2
let someData = [indexPath.row]
//Set up labels etc.
return cell!
}
}
到目前为止我有以下代码。
var someData = [SomeData]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2
let someData = [indexPath.row]
//Set up labels etc.
return cell!
}
}
我需要 Cell1,它是一个静态单元格,并且将始终保持在 indexPath 0 以位于名为 "Section1" 的部分中,并且所有 Cell2 都位于名为 "Section2"[ 的部分中=12=]
其他数据源和委托方法;
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else {
return someData.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Section1" }
else {
return "Section2"
}
}
这 returns 我需要第一部分所需的一切,但是,当涉及到第二部分时(由于 cellForRowAtIndex 中某处的代码),第 2 部分在 indexPath 0 处包含 Cell2。
非常感谢任何帮助。
根本原因:
在 cellForRowAtIndexPath
中检查 indexPath.section
而不是 indexPath.row
修复:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! Cell1
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as? Cell2
let someData = [indexPath.row]
//Set up labels etc.
return cell!
}
}