比较列表中的数组和选定的值,并在表格单元格基于索引
问题描述:
显示它我有一个应用程序,其中当tableview单元格被点击时,另一个tableview被加载,并进行api调用。基于api的响应,加载表视图列表,并且当第二个tableview中的特定项目被选中时,除tableview文本标签之外还显示一个选定的复选框,同时数据库更新为选定值 当我回到第一个tableview时,我会显示带有选定项目的标签。比较列表中的数组和选定的值,并在表格单元格基于索引
当我点击第一个tableview单元格时,会调用api,并将api的结果与数据库中的活动列表进行比较,并且该特定单元格应该保持选中状态。
当在第一个tableview中选择了某个项目,并且当我单击该特定单元格时,api结果会重新加载tableview,并且不会显示对各个单元格的选择。
以下是代码:
for selectedDict in (appDelegate?.selectedCategoryFilterArray)! {
let selectedUuid = selectedDict.categoryUuid
print("selectedUuid\(selectedUuid)")
for allDict in self.requestedFiltersArray!{
let allUuid = allDict.objectForKey("uuid") as? String
if selectedUuid == allUuid {
cell.imgSelected.image = UIImage(named: "radio_selected")
continue
}else{
cell.imgSelected.image = UIImage(named: "radio")
}
print("allUuid\(allUuid)")
}
}
这工作不正常,无电池作为选择,即使它们是选定单元格显示。
答
你把这段代码放在哪里?
从我认为你可能试图实现的方面来说,这里是我建议的方法。
对于基于API调用结果加载的第二个tableView,我会将一个名为uuid的String属性添加到您的自定义UITableViewCell类中。
然后,当您调用cellForRowAtIndexPath来填充第二个tableView时,将每个单元格实例化为您的自定义UITableViewCell,并使用indexPath.row将其uuid属性设置为基于结果数组的适当值。
设置完之后,接下来可以运行for循环,以便将单元格的uuid属性值与AppDelegate中的selectedUuid值匹配,然后根据逻辑设置图像。
粗略实现:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! YourCustomTableViewCell
let allDict = self.requestedFiltersArray[indexPath.row]
let allUuid = allDict.objectForKey("uuid") as? String
cell.uuid = allUuid
for selectedDict in (appDelegate?.selectedCategoryFilterArray)! {
let selectedUuid = selectedDict.categoryUuid
if selectedUuid = cell.uuid {
cell.imageSelected.image = UIImage(named: "radio_selected")
} else {
cell.imageSelected.image = UIImage(named: "radio")
}
}
}
嘿感谢,它与一个小的变化工作。必须在if条件中添加一个中断。 – Rani