gpt4 book ai didi

swift - 在标题部分 TableView 中返回标题标题

转载 作者:行者123 更新时间:2023-11-30 11:56:45 26 4
gpt4 key购买 nike

我可以轻松返回标题部分的标题,例如:

if(section == 1) {
}

等等

我有这个:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

if let UpperView = Bundle.main.loadNibNamed("Head", owner: self, options: nil)?.first as? Head {
let sectionTitle = ["Head0","Head1","Head2"]
var mysection = 0
for sectitile in sectionTitle
{
if(section == mysection)
{

UpperView.lhlHead.text = sectitile
mysection += 1

}

}
return UpperView
}
return nil

}

我想在标题 View 的标签中设置Head1、Head2、Head3。

当我使用这个时它工作正常:

if(section == 0) {
UpperView.lhlHead.text = "Head0"
} else if(section == 1) {
UpperView.lhlHead.text = "Head1"
} else if(section == 2) {
UpperView.lhlHead.text = "Head2"
}

当使用数组时,看不到任何字符串。为什么会发生这种情况?

最佳答案

您的变量 mysection 始终设置为 0,但它与当前节的编号进行比较,当前节的编号从 0 到表中的节数不等。因此,检查 section == mysection 将仅针对第一部分的标题通过,并且您将仅将标题设置到第一部分的标题中。

相反,此函数为您提供已处理的节号 (section: Int),并且您有 sectionTitle 数组,该数组与节号(数组索引)匹配部分的标题。所以代替:

if let UpperView = Bundle.main.loadNibNamed("Head", owner: self, options: nil)?.first as? Head {
let sectionTitle = ["Head0","Head1","Head2"]
var mysection = 0
for sectitile in sectionTitle
{
if(section == mysection)
{

UpperView.lhlHead.text = sectitile
mysection += 1

}

}
return headerView
}
return nil

你可以使用这个:

if let UpperView = Bundle.main.loadNibNamed("Head", owner: self, options: nil)?.first as? Head {
let sectionTitle = ["Head0","Head1","Head2"]
UpperView.lhlHead.text = sectionTitle[section]
return headerView
}
return nil

另外,变量名不要大写,所以正确的代码是这样的:

if let upperView = Bundle.main.loadNibNamed("Head", owner: self, options: nil)?.first as? Head {
let sectionTitle = ["Head0","Head1","Head2"]
upperView.lhlHead.text = sectionTitle[section]
return headerView
}
return nil

关于swift - 在标题部分 TableView 中返回标题标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47727702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com