Вы должны объявить массив как:
var checked = [Bool]()
Затем добавьте эту строку кода в вызов API, где вы получите данные в массиве
self.checked = Array(repeating: false, count: self.arraydata.count)
В методе делегата табличного представления:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell:switchTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! switchTableViewCell
//configure you cell here.
if checked[indexPath.row] == false{
cell.accessoryType = .none
} else if checked[indexPath.row] {
cell.accessoryType = .checkmark
}
cell.title.text = self. arraydata[indexPath.row]["amp_id"].string
return cell
}
добавьте еще один метод делегата:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark {
cell.accessoryType = .none
checked[indexPath.row] = false
} else {
cell.accessoryType = .checkmark
checked[indexPath.row] = true
}
}
on OKClickedButtonAction:
serviceString = ""
for i in 0..<checked.count{
if checked[i] == true{
serviceString = serviceString + self.arraydata[i]["emp_id"].string! + ", "
print(serviceString)
}
}
if serviceString == ""{
self.servicesBtnLbl.text = "Tap to select"
}else{
self.servicesBtnLbl.text = serviceString
}
Это работает Решение, надеюсь, оно будет вам полезно.