如何使用数组中已有的值制作表视图行 select。在 swift

How to make tableview rows select with already existing values in Array. in swift

细胞模型

class LangModel{
var name: String?
var id: Int?
var isSelected: Bool?

init(name: String?, id: Int?, isSelected: Bool?) {
    self.name = name
    self.id = id
    self.isSelected = isSelected
}
}

我正在从 JSON 向表视图添加值,如下所示

for langData in getuserData?.result?.language ?? []{

   self.langCellArray.append(LangModel(name: langData.language ?? "", id: langData.id ?? 0, isSelected: false))
  }
 tableView.reloadData()

使用下面的代码,当我单击 languagesBtn 和 select 并删除 select 表格视图行时,我能够显示表格视图..

但我需要的是..如果languageTextField.text包含语言那么如果我点击languagesBtn然后tableview行应该selected with languageTextField text

怎么做

showPop 显示带有“完成”和“取消”按钮的表格视图以及 languageTextField is inside languagesBtn 在设计中

@IBAction func languagesBtn(_ sender: Any) {
    showPop()
}

@IBAction func cancelBtn(_ sender: Any) {
remove()
}

@IBAction func doneBtn(_ sender: Any) {

languageTextField.text = selectedlangArray.joined(separator: ", ")
print("Lang array \(selectedlangArray)")
remove()
tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return langCellArray.count
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: "EditLangTableVIewCell", for: indexPath) as! EditLangTableVIewCell

cell.langLbl.text = langCellArray[indexPath.row].name

let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
cell.accessoryType = rowIsSelected ? .checkmark : .none

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell:EditLangTableVIewCell = tableView.cellForRow(at: indexPath) as! EditLangTableVIewCell

cell.accessoryType = .checkmark
langCellArray[indexPath.row].isSelected = true
selectedlangArray.append((cell.langLbl?.text)!)

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell:EditLangTableVIewCell = tableView.cellForRow(at: indexPath) as! EditLangTableVIewCell
cell.accessoryType = .none
}

请帮忙

当您调用 tableView.reloadData() 时,它会清除 UITableView 实例中所有先前选择的 indexPath 值。

此问题的一个解决方法是替换以下 -

let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)

let rowIsSelected = (langCellArray[indexPath.row].isSelected == true)

还有-

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell: EditLangTableVIewCell = tableView.cellForRow(at: indexPath) as! EditLangTableVIewCell
    cell.accessoryType = .none

    langCellArray[indexPath.row].isSelected = false
}

更新

如果我们想继续使用 indexPathsForSelectedRows 中内置的 UITableView 来跟踪所选状态,我们可以放弃我上面解释的 rowIsSelected 的更改。

修复方法是 - 在重新加载后手动重新选择所有先前选择的索引路径 -

tableView.reloadData()

for (index, model) in langCellArray.enumerated() {
    if model.isSelected == true {
        let ip = IndexPath(row: index, section: 0)
        tableView.selectRow(at: ip, animated: false, scrollPosition: .none)
    }
}

因为你在模型中有一个属性 isSelected,使用它(并声明它是非可选的,没有maybe-state ).

selectedIndexPaths 以及额外的数组根本不需要。 删除它们。

您甚至可以使用结构(初始化程序是免费的)

struct LangModel {
    var name: String?
    var id: Int?
    var isSelected: Bool
}

cellForRowAt中根据isSelected设置附件视图。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        
    let cell = tableView.dequeueReusableCell(withIdentifier: "EditLangTableVIewCell", for: indexPath) as! EditLangTableVIewCell
    
    let item = langCellArray[indexPath.row]
    cell.langLbl.text = item.name
    cell.accessoryType = item.isSelected ? .checkmark : .none
    
    return cell
}

并在 didSelect 中切换 isSelected 并仅重新加载该行。不要直接修改单元格,告诉 cellForRow 通过调用 reloadRows(at:with:)

来完成
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    langCellArray[indexPath.row].isSelected.toggle()
    tableView.reloadRows(at: [indexPath], with: .automatic) 
}

也删除

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell:EditLangTableVIewCell = tableView.cellForRow(at: indexPath) as! EditLangTableVIewCell
    cell.accessoryType = .none
}