简体   繁体   English

斯威夫特| TableView为空/空白(使用自定义单元格)

[英]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. 我的TableView是空的/只显示了空白单元格(屏幕快照)我尝试了使用TableViewController和使用TableView的ViewController两者-都显示了相同的结果。 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. 因此,我试图确定问题的确切位置,并且似乎未读“ for-statement”。 (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.) 我刚开始编码,但这没有任何意义,我在这里尝试了所有关于stackoverflow的好建议,但是似乎都没有用(所有插座都已连接等)。

Ill post both variations: ViewController and the TableViewController (which both should get the same info) 同时发布两个变体:ViewController和TableViewController(它们都应获得相同的信息)

TableViewController: 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 在控制台中,它显示以下内容: 因此似乎未读for语句

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: 这也是ViewController的另一个变体,并添加了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. 您没有将表视图委托设置为self,这就是它发生的原因。 In the ViewController and added TableView: ViewController中添加TableView:

  1. In viewDidLoad set self.tableView.delegate = self. 在viewDidLoad中设置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 将您的客户单元格注册到表视图中,例如tableView.registerNib(UINib(nibName: "nameOfTheNib", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")示例tableView.registerNib(UINib(nibName: "nameOfTheNib", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
  3. in cellForRowAtIndexPath create that cell using the "reuseIdentifier" cellForRowAtIndexPath ,使用"reuseIdentifier"创建该单元格
  4. It will work 会工作的

You need to add this code in your cellForRowAtIndexPath 您需要在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:- 也像这样注册您xib:-

override func viewDidLoad() {
    super.viewDidLoad()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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