在经历了大约一千个问题之后,我已经放弃了并且正在寻求帮助。我有一个非常简单的表使用自定义单元格。按下按钮时,会向阵列中添加一个新项目,并且表格应更新以显示新项目。那没有发生。数据全部正确移动,但让表更新的唯一方法是退出并重新启动应用程序。下面的代码中删除了关于定义变量等的不必要的部分。谢谢!
更新:我删除,重新创建并重新链接了表格和原型单元格而没有任何变化。
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
}
}
答案 0 :(得分:2)
.beginUpdates()
/ .endUpdates()
实际上不会导致表视图重新加载它显示的内容。
您正在更新数据源,您应该通过调用you can list all of a user's gists让表格视图了解更改,以便它会向数据源询问您所做的更改并重新组织其内容: / p>
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()
}
答案 1 :(得分:-1)
我在按钮Could not deduce (IConnection (IO Connection))
arising from a use of ‘quickQuery'’
from the context: convertible-1.1.1.0:Data.Convertible.Base.Convertible
a SqlValue
bound by the inferred type of
getPerson :: convertible-1.1.1.0:Data.Convertible.Base.Convertible
a SqlValue =>
a -> IO [[SqlValue]]
中调用.reloadData()
但我没有从Realm检索新数据。