Warm tip: This article is reproduced from stackoverflow.com, please click
swift uitableview

How do I make cells in a table view use array for their data after the content of array has changed?

发布于 2020-03-27 10:15:12

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 :)

Questioner
Hubert Rzeminski
Viewed
103
PGDev 2019-07-03 21:19

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
}