I'm writing a short app in swift which has two screen. On screen one the user enters data and saves it then on screen two the data is displayed in a tableView
. However I cant seem to get the tableView
to adapt to the changed array
.
I've looked around and cant seem to find a solution, however I'm new to this so I might be looking in the wrong places.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! BunchOfCells
cell.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
tableview.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
if tasks.count != 0 {cell.label.text = tasks[indexPath.item]["Title"]}
return cell
}
func createTask(){
let decider = UserDefaults.standard.object(forKey: "buttonPressed") as? Bool
if (decider == true){
let nameOfTask = UserDefaults.standard.object(forKey: "taskName") as! String
let importanceOfTask = UserDefaults.standard.object(forKey: "importance") as! String
let dateOfTask = UserDefaults.standard.object(forKey: "taskDate") as! String
let desOfTask = UserDefaults.standard.object(forKey: "taskDescription") as! String
taskInfo["Title"] = nameOfTask
taskInfo["Importance"] = importanceOfTask
taskInfo["Date"] = dateOfTask
taskInfo["Description"] = desOfTask
tasks.append(taskInfo)
print(tasks)
}
var tasks: [[String:String]] = [] as Array
var taskInfo: [String:String] = ["Title":"", "Importance":"", "Date":"", "Description":""]
let tableview: UITableView = {
let tv = UITableView()
tv.translatesAutoresizingMaskIntoConstraints = false
tv.separatorColor = UIColor.white
return tv
}()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let NumOfCell = tasks.count
return NumOfCell
}
tasks
is the array
storing a multiple dictionaries
with similar content.
My problem is that when another dictionary
is added to the array
the cells
in the table view don't change. Any help would be appreciated :)
Call reloadData()
on tableView
after you created a new task, i.e.
func createTask(){
let decider = UserDefaults.standard.bool(forKey: "buttonPressed")
if decider {
//your rest of the code...
}
tableView.reloadData()
}
Don't forget to create an outlet
for tableView
in your viewController
.
@IBOutlet weak var tableView: UITableView!
And ofcourse, as @vadian suggested, tableView(_:cellForRowAt:)
implementation must be like,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! BunchOfCells
cell.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0)
cell.label.text = tasks[indexPath.item]["Title"]
return cell
}
Actually there must be an outlet. Note the spelling in
cellForRow
😉.@vadian Didn't get you. Did I make any error in the code?
No, you didn’t but obviously there is an outlet
tableview
(with lowercase v) in the class.@vadian oh ok. Got that. Thanks for the catch. I updated that in the answer. Actually I used the tableView parameter in the method itself.
It’s highly recommended to use always the given
tableView
parameter in the data source and delegate methods.