'UIKit - removing rows from sections
I'm new to Swift, trying to build a simple toDoList using UIKit. For now, I'm trying to approach it the simplest way possible, so the data is appended in viewDidLoad. Unfortunately, I cannot wrap my mind around how to swipe delete sections and rows. For a starter, could you provide me with a suggestion what to type in editingStyle to remove the row? Or perhaps there's a better way to store the sections and corresponding rows? Also, I know that you can remove rows with leading/trailingSwipeActions, which way is easier?
List:
var tasksData = [Data]()
Data:
tasksData.append(Data.init(list: "For today", task: ["Do homework", "Go to school"]))
tasksData.append(Data.init(list: "For tomorrow", task: ["Relax", "Do nothing"]))
Delete row function - What should I type in the place of the question marks?:
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()
tasksData.removeAt ????
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
}
Cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell2 = AllTasksTableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! AllTasksCell
cell2.setup1(with: (tasksData[indexPath.section].task?[indexPath.row])!)
///cell2.textLabel?.text = tasksData[indexPath.section].task?[indexPath.row]
return cell2
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
