swift - 更改 uiTableViewCell 中选定单元格的选中状态

标签 swift uitableview

我是 swift 新手,正在尝试更改选中的选定 tableviewcell。 我试图;用户选择城市和地区,然后单击保存按钮,应用程序将信息发送到服务器。这样就没有问题了。当用户想要更新城市或地区时,我应该在地区之前检查,用户可以看到他/她选择或不选择什么。我使用 json 从服务器获取第一个信息并将它们附加到数组中。当用户单击更新按钮时,我可以在 Debug模式下看到所选信息之前,但表格 View 单元格不会被选中。顺便说一句,抱歉我的英语不好。

第一次选择我在做什么:

in viewDidLoad()
        self.tableViewDistrict.isEditing = true
        self.tableViewDistrict.allowsMultipleSelection = true
        self.tableViewDistrict.allowsMultipleSelectionDuringEditing = true

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.selectDeselectCell(tableView: tableView, indexPath: indexPath)
    }

extension ViewController{
    func selectDeselectCell(tableView: UITableView, indexPath: IndexPath){
        self.selectedDistId.removeAll()
        if let array = tableView.indexPathsForSelectedRows{
            for index in array{
                selectedDistId.append(districtArray[index.row].id!)
            }        
        }
    }
}

最佳答案

假设您已将之前选择的所有数据存储在一个数组中,例如

var alreadySelectedDistricts : [String] = []

因此,在您的 cellForRowAt 方法中,您可以进行类似的检查

for district in alreadySelectedDistricts { 
    if cell.districtLabel.text.contains(district) { 
        cell.accessoryType = .checkmark
    } else { 
        cell.accessoryType = .none
    }
}

关于swift - 更改 uiTableViewCell 中选定单元格的选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59209678/

相关文章:

ios - 如何从 nsdictionary 中获取选定值的键

ios - Swift 中 ru_RU 的 UITextChecker

ios - kCFCompareEqualTo 在 swift 中不可用吗?

arrays - 如何将元组数组转换为字符串?

ios - UI 测试滑动删除表格 View 单元格

ios - 当文本字段没有 IBOutlet 时关闭键盘

ios - Swift - UIImageView 缩放比 UIScrollView 大

uitableview - prepareForSegue 错误 Thread 1 : EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)

iphone - UIImageView 不一致地变黑

iphone - 重新加载 UITableView 标题 View ?