【问题标题】:Comparing an list of array and selected value and displaying it in tableview cell based on index比较数组列表和选定值并根据索引将其显示在表格视图单元格中
【发布时间】:2018-03-05 11:35:05
【问题描述】:

我有一个应用程序,其中单击 tableview 单元格时,会加载另一个 tableview 并进行 api 调用。 Based on the response from api, table view list is loaded and when a particular item in the second tableview is selected, there is a selected checkbox displayed just besides the tableview text label and at the same time database is updated with selected value, 所以当我回到第一个表格视图时,我会显示一个带有所选项目的标签。

当我点击第一个表格视图单元格时,会调用 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)")
                }
            }

这没有按预期工作,没有单元格显示为选中状态,即使它们是选中的单元格。

【问题讨论】:

    标签: ios swift tableview


    【解决方案1】:

    你把这段代码放在哪里了?

    根据我认为您可能想要实现的目标,这是我建议的一种方法。

    对于根据 API 调用结果加载的第二个 tableView,我将在自定义 UITableViewCell 类中添加一个名为 uuid 的字符串属性。

    然后,当您调用 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 条件中添加一个中断。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多