Swift UITableView中带有自定义单元格的部分
到目前为止,我有以下代码Swift UITableView中带有自定义单元格的部分,swift,uitableview,sections,indexpath,Swift,Uitableview,Sections,Indexpath,到目前为止,我有以下代码 var someData = [SomeData]() func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for:
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,它是一个静态单元格,并且始终保持在0,以便位于一个名为Section1的节中&例如,所有Cell2都位于一个名为Section2的节中
其他数据源和委托方法
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"
}
}
这将返回第一节所需的所有内容,但是,当涉及到第二节时,由于CellForRowatinex中的代码,第2节包含位于0处的Cell2
非常感谢您的帮助。根本原因:
在cellForRowAtIndexPath中,检查indexPath.section而不是indexPath.row
修正:
根本原因:
在cellForRowAtIndexPath中,检查indexPath.section而不是indexPath.row
修正:
在cellForRowAtIndexPath检查indexPath.section而不是indexPath.RowThank中,@user1046037在第二节的索引0上有一些异常行为,但意识到我需要参考该节以及关于indexPath方法的高度。请添加作为答案,我会接受。在cellForRowAtIndexPath检查indexath.section而不是indexath.RowThank中,@user1046037在第二节的索引0上有一些异常行为,但意识到我需要参考该节以及我的身高以了解indexath方法。请加上答案,我会接受的。
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!
}
}