我是 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/