FasterThanLlamas
FasterThanLlamas

Reputation: 113

UITableView won't update

After going through about a thousand SO questions, I've given up and am reaching out for help. I have a very straightforward table using custom cells. When a button is pushed, a new item is added to the array, and the table should update to show the new item. That's not happening. The data is all moving correctly but the only way to get the table to update is to quit and relaunch the app. Code below with unnecessary sections about defining variables, etc. removed. Thanks!

UPDATE: I have deleted, recreated, and relinked the table and prototype cell with no change.

class DispatchTableViewCell: UITableViewCell {

@IBOutlet weak var routeLabel: UILabel!
@IBOutlet weak var aircraftLabel: UILabel!
@IBOutlet weak var altitudeLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var departureTimeLabel: UILabel!
@IBOutlet weak var arrivalTimeLabel: UILabel!
@IBOutlet weak var remainingTimeLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!

}





class DispatchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var flightsTableView: UITableView!

@IBAction func dispatchButton(_ sender: Any) {

    flightsTableView.beginUpdates()

// Do all the things to update the array
// Works fine, array is correct

    flightsTableView.endUpdates()

}

let realm = try! Realm()

var flightsArray: [Flight] = []
var originsArray: [String] = []
var destinationsArray: [String] = []
var aircraftNamesArray: [String] = []
var departureTimesArray: [Date] = []
var arrivalTimesArray: [Date] = []
var speedsArray: [Double] = []
var altitudesArray: [Double] = []

override func viewDidLoad() {
    flightsTableView!.dataSource = self
    flightsTableView!.delegate = self
    super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {

flightsTableView.beginUpdates()
    for flight in realm.objects(Flight.self) {
        flightsArray.append(flight)
    }
    for flight in flightsArray {
        originsArray.append(flight.originCode)
        destinationsArray.append(flight.destinationCode)
        aircraftNamesArray.append(flight.aircraftNameShort)
        departureTimesArray.append(flight.departureTime)
        arrivalTimesArray.append(flight.arrivalTime)
        speedsArray.append(flight.cruiseSpeed)
        altitudesArray.append(flight.cruiseAltitude)
    }
    flightsTableView.endUpdates()

}

func numberOfSections(in tableView: UITableView) -> Int {

    return 1

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return flightsArray.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return 90
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "flight", for: indexPath) as! DispatchTableViewCell

    let origin = originsArray[indexPath.row]
    let destination = destinationsArray[indexPath.row]

    cell.routeLabel?.text = "\(origin) -> \(destination)"
    print(cell.routeLabel)
    cell.aircraftLabel?.text = aircraftNamesArray[indexPath.row]

    cell.altitudeLabel?.text = "Altitude: \(Int(altitudesArray[indexPath.row]))"

    cell.speedLabel?.text = "Speed: \(Int(speedsArray[indexPath.row]))"

    cell.statusLabel?.text = "Test"

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"
    dateFormatter.timeZone = TimeZone.current
    //tempFlight.departureTime = dateFormatter.string(from: tempFlight.departureTime)
    //tempFlight.arrivalTime = dateFormatter.string(from: tempFlight.arrivalTime)

    cell.departureTimeLabel?.text = dateFormatter.string(from: departureTimesArray[indexPath.row])

    cell.arrivalTimeLabel?.text = dateFormatter.string(from: arrivalTimesArray[indexPath.row])

    return cell


}

}

Upvotes: 0

Views: 215

Answers (2)

FasterThanLlamas
FasterThanLlamas

Reputation: 113

I was calling .reloadData() in the button func but I was not retrieving fresh data from Realm.

Upvotes: -1

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52153

.beginUpdates()/.endUpdates() does not actually cause table view to reload the content it displays.

You are updating the data source and, you should let table view know about the changes by calling .reloadData() so it will ask the data source for the changes you've made and reorganize its own content:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    for flight in realm.objects(Flight.self) {
        flightsArray.append(flight)
    }

    for flight in flightsArray {
        originsArray.append(flight.originCode)
        destinationsArray.append(flight.destinationCode)
        aircraftNamesArray.append(flight.aircraftNameShort)
        departureTimesArray.append(flight.departureTime)
        arrivalTimesArray.append(flight.arrivalTime)
        speedsArray.append(flight.cruiseSpeed)
        altitudesArray.append(flight.cruiseAltitude)
    }

    flightsTableView.reloadData()
}

Upvotes: 2

Related Questions