简体   繁体   中英

Swift | TableView is Empty / Blank (using Custom Cell)

My TableView is empty/ just shows the blank cells (screenshot) I tried it both with a TableViewController and a ViewController with a TableView - both show the same result. enter image description here

So I tried to identify where excactly the problem is located and it seems that a "for-statesment" is not being read. (see the checks printed)

Im new to coding, but this just doesn't make any sense, I have tried all the great suggestion here on stackoverflow but none seem to work (all outlets are connected etc. etc.)

Ill post both variations: ViewController and the TableViewController (which both should get the same info)

TableViewController:

import UIKit
import Parse

class HistoryTableViewController: UITableViewController {

var zeit = [String]()
var kosten = [String]()
var kilometer = [String]()
var datum = [String]()


override func viewDidLoad() {
    super.viewDidLoad()


    let query = PFQuery(className: "GemachteFahrten")

    query.whereKey("gefahreneZeit", equalTo: (PFUser.current()?.objectId!)!)

    query.whereKey("kosten", equalTo: (PFUser.current()?.objectId!)!)

    query.whereKey("createdAt", equalTo: (PFUser.current()?.objectId!)!)

    query.whereKey("gefahreneKilometer", equalTo: (PFUser.current()?.objectId!)!)

    print("check1")

    query.findObjectsInBackground { (objects, error) in

        if error != nil {

            print("error finding user data")


        } else {

            print("check2")
            if let fahrtenRequests = objects {

                self.zeit.removeAll()
                self.datum.removeAll()
                self.kosten.removeAll()
                self.kilometer.removeAll()

                print("check3")


                for fahrtenRequest in fahrtenRequests {

                    print("check4")

                    self.zeit.append(fahrtenRequest["gefahreneZeit"] as! String!)
                    self.kosten.append(fahrtenRequest["kosten"] as! String)
                    self.datum.append(fahrtenRequest["createdAt"] as! String)
                    self.kilometer.append(fahrtenRequest["gefahreneKilometer"] as! String)

                    self.tableView.reloadData()

                    print("check5")

                }
                print("check6")

            }
         }
    }

    self.tableView.reloadData()
}

override func viewDidAppear(_ animated: Bool) {

}

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 {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return datum.count
}


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


            cell.durationCell.text = zeit[indexPath.row]

            cell.costCell.text = kosten[indexPath.row]
            cell.routeCell.text = kilometer[indexPath.row]

            cell.dateCell.text = datum[indexPath.row]

            return cell

}

In the console it prints this: So it seems the for-statement is not read

So it seem this is the problem? Or did I do something wrong somewhere else?:

print("check3")


                for fahrtenRequest in fahrtenRequests {

                    print("check4")

                    self.zeit.append(fahrtenRequest["gefahreneZeit"] as! String!)
                    self.kosten.append(fahrtenRequest["kosten"] as! String)
                    self.datum.append(fahrtenRequest["createdAt"] as! String)
                    self.kilometer.append(fahrtenRequest["gefahreneKilometer"] as! String)

                    self.tableView.reloadData()

                    print("check5")

                }
                print("check6")

Also here is the other variation with ViewController and added TableView:

import UIKit
import Parse

class HistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var zeit = [String]()
var kosten = [String]()
var kilometer = [String]()
var datum = [String]()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    let query = PFQuery(className: "GemachteFahrten")

    query.whereKey("gefahreneZeit", equalTo: (PFUser.current()?.objectId!)!)

    query.whereKey("kosten", equalTo: (PFUser.current()?.objectId!)!)

    query.whereKey("createdAt", equalTo: (PFUser.current()?.objectId!)!)

    query.whereKey("gefahreneKilometer", equalTo: (PFUser.current()?.objectId!)!)

    print("check1")

    query.findObjectsInBackground { (objects, error) in

        if error != nil {

            print("error finding user data")

        } else {

            print("check2")
            if let fahrtenRequests = objects {

                self.zeit.removeAll()
                self.datum.removeAll()
                self.kosten.removeAll()
                self.kilometer.removeAll()

                print("check3")


                for fahrtenRequest in fahrtenRequests {

                    print("check4")

                    self.zeit.append(fahrtenRequest["gefahreneZeit"] as! String!)
                    self.kosten.append(fahrtenRequest["kosten"] as! String)
                    self.datum.append(fahrtenRequest["createdAt"] as! String)
                    self.kilometer.append(fahrtenRequest["gefahreneKilometer"] as! String)

                    self.tableView.reloadData()

                    print("check5")

                }
                print("check6")

            }


        }

    }

    self.tableView.reloadData()
   }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}



internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows

         return datum.count
}


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


    cell.durationCell.text = zeit[indexPath.row]

    cell.costCell.text = kosten[indexPath.row]
    cell.routeCell.text = kilometer[indexPath.row]

    cell.dateCell.text = datum[indexPath.row]

    return cell
   }

Also here is the Custom-Cell Code:

import UIKit

class CellHistoryViewControllerTableViewCell: UITableViewCell {


@IBOutlet weak var dateCell: UILabel!

@IBOutlet weak var durationCell: UILabel!

@IBOutlet weak var costCell: UILabel!

@IBOutlet weak var routeCell: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
 }
}

I really hope somebody recognizes the problem, if you need more info or screenshots just tell me :)

Again THANKS for your help and time :)

you are not setting table view delegate to self that is why its happening. In the ViewController and added TableView:

  1. In viewDidLoad set self.tableView.delegate = self.
  2. register your customer cell to table view like example tableView.registerNib(UINib(nibName: "nameOfTheNib", bundle: nil), forCellReuseIdentifier: "reuseIdentifier") in viewDidLoad
  3. in cellForRowAtIndexPath create that cell using the "reuseIdentifier"
  4. It will work

You need to add this code in your cellForRowAtIndexPath

if cell == nil {
    cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellIdentifier)
}

So your method would be -

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

  if cell == nil {
    cell = CellHistoryViewControllerTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CellHistory")
}
    cell.durationCell.text = zeit[indexPath.row]

    cell.costCell.text = kosten[indexPath.row]
    cell.routeCell.text = kilometer[indexPath.row]

    cell.dateCell.text = datum[indexPath.row]

    return cell



}

Also register you xib like this:-

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.registerNib(UINib(nibName: "CellHistoryViewControllerTableViewCell", bundle: nil), forCellReuseIdentifier: "CellHistory")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM