Jestem totalnie nowy w temacie Swift, obecnie próbuję nauczyć się UIKit. Tworze todolist, w której pomysłem jest aby dla użytkownika wyświetlały się tylko te zadania z konkretnej listy, wybieranej z "dropdown menu" (przerobionego pickera). W skrócie index wybranej listy przesyłany jest do cellForRowAt, dzięki czemu mogę wyświetlać zadania tylko z konkretnej "listy". Jednak gdy usuwam jakiegoś taska, liczba rows w innych listach również zmniejsza się o 1. Wydaje mi się, że nie tak do tego podszedłem. Czy mogę prosić o jakąś poradę jak myśleć? Sorry za chaos w kodzie, ale tak jak mówiłem jestem na etapie eksperymentowania, jednak dalej nie pójdę jeśli tego problemu nie rozwiąże. Następnym krokiem ma być dodawanie/edytowanie tasków w poszczególnych "listach".
Kod:
import UIKit
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var AllTasksTableView: UITableView!
@IBOutlet weak var textF: UITextField!
@IBOutlet weak var numerListy: UILabel!
@IBOutlet weak var ListBox: UITextField!
@IBOutlet weak var listPicker: UIPickerView!
var i1 = 0
///PICKER
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//var listaZadan = namesOfLists()
var countRows = namesOfLists().count
if pickerView == listPicker {
countRows = self.namesOfLists().count
}
return countRows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == listPicker {
let titleRow = namesOfLists()[row]
if ListBox.text != "" {
let idx = daneZadania.firstIndex(where: { $0.lista == ListBox.text})!
numerListy.text = String(idx)
}
AllTasksTableView.reloadData()
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == listPicker {
self.ListBox.text = self.namesOfLists()[row]
//self.listPicker.isHidden = true
}
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.ListBox {
//self.listPicker.isHidden = false
}
}
@IBAction func delList(_ sender: Any) {
if ListBox.text != "" {
daneZadania.removeAll(where: { $0.lista == ListBox.text})
}
AllTasksTableView.reloadData()
}
@IBAction func remove(_ sender: Any) {
if ListBox.text != "" {
let idx = daneZadania.firstIndex(where: { $0.lista == ListBox.text})!
numerListy.text = String(idx)
}
AllTasksTableView.reloadData()
}
var zadanie1: Dane!
var daneZadania = [Dane]()
override func viewDidLoad() {
super.viewDidLoad()
AllTasksTableView.delegate = self
AllTasksTableView.dataSource = self
if zadanie1 == nil {
zadanie1 = Dane(lista: "For friday", zadanie: ["get a life", "find a friend"], completed: false)
}
daneZadania.append(Dane.init(lista: "For today", zadanie: ["Do homework", "Go to school"], completed: false))
daneZadania.append(Dane.init(lista: "For tomorrow", zadanie: ["Relax", "Do nothing"], completed: false))
daneZadania.append(zadanie1)
}
//TASKS NAMES
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell2 = AllTasksTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! AllTasksCell
let liczba = Int(numerListy.text ?? "0")!
cell2.ATLabel.text = daneZadania[liczba].zadanie?[indexPath.row]
cell2.checkBoxButton.isSelected = daneZadania[indexPath.row].completed
cell2.accessoryType = cell2.isSelected ? .checkmark : .none
cell2.selectionStyle = .none // to prevent cells from being "highlighted"
return cell2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return daneZadania[section].zadanie?.count ?? 0
//return daneZadania[section].zadanie?.count ?? 0
}
///DEL
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// tableView.beginUpdates()
daneZadania[indexPath.section].zadanie?.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
//tableView.endUpdates()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func namesOfLists () -> [String] {
let nazwy = daneZadania.map({$0.lista!})
return nazwy
}
}