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

时间:2016-10-15 09:39:51

标签: ios arrays swift uitableview tableviewcell

我的TableView为空/只显示空白单元格(屏幕截图) 我用TableViewController和带有TableView的ViewController都试了一下 - 两者都显示了相同的结果。 enter image description here

所以我试图找出问题的确切位置,似乎是一个" for-statement"没有被阅读。 (见打印的支票)

我是编码的新手,但这没有任何意义,我已经在stackoverflow上尝试了所有伟大的建议,但似乎没有工作 (所有插座都连接等等。)

发布两种变体:ViewController和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

}

在控制台中打印出来: So it seems the for-statement is not read

所以看来这是问题所在?或者我在其他地方做错了什么?:

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")

此处是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
   }

此处还有自定义单元格代码:

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
 }
}

我真的希望有人认识到这个问题,如果您需要更多信息或截图,请告诉我:)

再次感谢您的帮助和时间:)

2 个答案:

答案 0 :(得分:0)

您没有将表视图委托设置为self,这就是它发生的原因。 在 ViewController中添加了TableView:

  1. 在viewDidLoad中设置self.tableView.delegate = self。
  2. 将您的客户单元注册到表格视图,例如viewDidLoad
  3. 中的示例tableView.registerNib(UINib(nibName: "nameOfTheNib", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
  4. cellForRowAtIndexPath中使用"reuseIdentifier"
  5. 创建该单元格
  6. 它会起作用

答案 1 :(得分:0)

您需要在cellForRowAtIndexPath

中添加此代码
if cell == nil {
    cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellIdentifier)
}

所以你的方法是 -

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



}

还要像这样注册xib: -

override func viewDidLoad() {
    super.viewDidLoad()

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