UITableView与自定义单元格中的部分
问题描述:
到目前为止我有以下代码。UITableView与自定义单元格中的部分
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!
}
}
我需要小区1这是一个静态的细胞,并始终保持在indexPath 0是在一个名为“SECTION1”例如&所有的小区2的部分是在一个被称为“第2节”
节其他数据源&委托方法;
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"
}
}
这将返回我的一切,我需要为第一部分,然而,当涉及到第二部分(因为内部cellForRowAtIndex某处的代码)第2条中包含小区2在indexPath 0
任何帮助不胜感激。
答
根源:
在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!
}
}
在'cellForRowAtIndexPath'检查'indexPath.section'而不是'indexPath.row' – user1046037
谢谢@ user1046037在第二部分的索引0处有一些不寻常的行为,但是我意识到我需要在indexPath方法的高度上引用该部分。请添加为答案,并接受它。 –