У меня есть tableView
с section headers
, и я хочу добавить определенный пользовательский ввод в несколько выбранных headers
.Я создал section headers
и могу изменить изображение на section header
, чтобы показать, что оно выбрано, но я хочу создать array
из выбранных.
Вот мой код для section header
:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let userModel = Data.userModels[section]
let cell = tableView.dequeueReusableCell(withIdentifier: "nameCell") as! NameHeaderTableViewCell
cell.setup(model: userModel)
cell.checkMarkButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
cell.enable(on: false)
if isUserEditing == true {
cell.enable(on: true)
}
return cell.contentView
}
Здесь я могу изменить section image
, когда пользователь нажимает на раздел:
@objc func handleTap(sender: UIButton) {
sender.isSelected = !sender.isSelected
}
Когда пользователь нажимает кнопку сохранения button
, я хочу, чтобы пользовательский ввод был добавлен к тем ячейкам, где выбран section header
.Вот этот код:
@IBAction func save(_ sender: Any) {
//VALIDATION
guard mealItemTextField.text != "", let item = mealItemTextField.text else {
mealItemTextField.placeholder = "please enter an item"
mealItemTextField.layer.borderWidth = 1
mealItemTextField.layer.borderColor = UIColor.red.cgColor
return
}
guard priceTextField.text != "", let price = Double(priceTextField.text!) else {
priceTextField.placeholder = "please enter a price"
priceTextField.layer.borderWidth = 1
priceTextField.layer.borderColor = UIColor.red.cgColor
return
}
tableView.reloadData()
}
В настоящее время я застрял на том, как получить доступ ко всем indexes
из sections
, которые выбраны (т.е. те, которые имеют состояние выбранного) Вот некоторыеснимки экрана, помогающие визуализировать программу:
PS Не уверен, что это полезно, но я заполняю данныеarray
из structs
.Вот этот код:
func numberOfSections(in tableView: UITableView) -> Int {
return Data.userModels.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if Data.userModels[section].isExpandable {
return Data.userModels[section].itemModels.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = Data.itemModels[indexPath.row].itemName
return cell!
}
Любая помощь приветствуется, спасибо!