我试图将我的JSON数据从我的服务器加载到我的UITableView中,但是当我调用ReloadData方法时,似乎没有发生任何事情(据我所知它应该调用CellForRowatIndexPath)。我已经尝试了一些我发现的解决方案(比如把它放在主线程中),但那些没有做到这一点。所以我想知道是否有人可以帮助我,因为我不知道该怎么做。
提前致谢。
这是我的代码:
import UIKit
import Alamofire
private let reuseIdentifier = "PresenceCell"
class PresenceController: UITableViewController {
@IBOutlet var presenceTableView: UITableView!
var presenceList = [] as [Presence]
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addPresence))
loadPresences(date: NSDate() as Date)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return presenceList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! PresenceCell
let presence = presenceList[indexPath.row]
cell.lblPresenceName.text = presence.name
cell.lblPresenceTimes.text = presence.times
if (presence.reasons.characters.contains("S")) {
cell.lblPresenceSTD.isHidden = true
} else {
cell.lblPresenceSTD.isHidden = false
}
if (presence.reasons.characters.contains("M")) {
cell.lblPresenceMDN.isHidden = true
} else {
cell.lblPresenceMDN.isHidden = false
}
if (presence.reasons.characters.contains("C")) {
cell.lblPresenceEDH.isHidden = true
} else {
cell.lblPresenceEDH.isHidden = false
}
if (presence.reasons.characters.contains("L")) {
cell.lblPresenceLEA.isHidden = true
} else {
cell.lblPresenceLEA.isHidden = false
}
if (presence.reasons.characters.contains("T")) {
cell.lblPresenceTRD.isHidden = true
} else {
cell.lblPresenceTRD.isHidden = false
}
if (presence.event) {
cell.swPresenceEvent.isOn = true
} else {
cell.swPresenceEvent.isOn = false
}
return cell
}
func addPresence(){
performSegue(withIdentifier: "addPresenceSegue", sender: self)
}
private func loadPresences(date: Date) {
let dateFormat = DateFormatter()
dateFormat.dateFormat = "dd-MM-yyyy"
let dateString = dateFormat.string(from: date)
let url = "MyUrl" + dateString
Alamofire.request(url, method: .get, encoding: URLEncoding.httpBody).responseJSON{ response in
if let json = response.result.value {
let dictionary = json as! [String:Any]
let presences = dictionary["presences"] as! NSArray
for index in 0..<presences.count {
let jsonPresence = presences[index] as! [String:Any]
let presence = Presence(id: jsonPresence["id"] as! Int, name: jsonPresence["name"] as! String, times: (jsonPresence["starttime"] as! String) + " - " + (jsonPresence["endtime"] as! String), reasons: jsonPresence["reasons"] as! String, event: ((jsonPresence["event"] as! Int) == 1))
//add your data into tables array from textField
self.presenceList.append(presence)
}
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
}
}
答案 0 :(得分:1)
看起来你的numberOfRowsInSection方法不太对劲。试试这个:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return presenceList.count
}