我有3个按钮可以从带有搜索功能的表格视图中选择3个项目。 一切正常,直到我搜索并选择一行。
当我选择row..it时,它会打印出“ nothing selected”,这是我包含在switch语句默认代码中的。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if isSearching == true{
tableData = filteredArray
switch filteredArray {
case signUser.countries:
signUser.country = tableData[indexPath.row]
self.btnCountry.titleLabel?.text = signUser.country
print(signUser.country)
case signUser.dawatTitles:
signUser.dawatName = tableData[indexPath.row]
self.btnDawat.titleLabel?.text = signUser.dawatName
case signUser.nationalities:
signUser.nationality = tableData[indexPath.row]
self.btnNationality.titleLabel?.text = signUser.nationality
default:
print("nothing selected")
}
}
else if isSearching == false{
tableData = unfilteredArray
switch unfilteredArray{
case signUser.countries:
signUser.country = tableData[indexPath.row]
print(signUser.country)
case signUser.dawatTitles:
signUser.dawatName = tableData[indexPath.row]
print(signUser.dawatName)
case signUser.nationalities:
signUser.nationality = tableData[indexPath.row]
print(signUser.nationality)
default:
print("dsds")
}
}
}
当我搜索并选择国家或任何项目时,它应该打印选定的国家或行。
无需搜索即可工作
答案 0 :(得分:1)
您可以执行按钮操作(在您编辑问题之前,我已经看到了此操作):
blower p2
reverse p3
并且在表格视图的开关中基本上选择了您
self.unfilteredArray = signUser.dawatTitles
// ...
self.unfilteredArray = signUser.countries
//...
self.unfilteredArray = signUser.nationalities
解决方案:
将按钮中的选择保留在变量中
类似:
//...
switch filteredArray {
case signUser.countries:
//...
// is the filteredArray the signUser.countries ?
// well … never because you never assing them
仍然...在代码中似乎还可以改善其他地方。