2017-08-29 108 views
0

我有問題,我的TableViewController不顯示單元格的內容。我有一個加載數據的按鈕,但它也應該顯示在單元格中。我在「vieDidLoad」功能中忘記了什麼嗎?您可以從故事板和應用程序的最後找到圖像。Swift TableView單元格不顯示

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.register(UINib(nibName: "TableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "TableViewCell") 
} 
override func viewWillAppear(_ animated: Bool) { 
    let btn = UIButton(frame: CGRect(x: 0, y: 25, width: self.view.frame.width, height: 50)) 
    btn.backgroundColor = UIColor.cyan 
    //btn.setTitle("Add new Dummy", for: UIControlState) 
    btn.addTarget(self, action: #selector(addDummyData), for: UIControlEvents.touchUpInside) 
    self.view.addSubview(btn) 
} 
@objc func addDummyData() { 
    RestApiManager.sharedInstance.getRandomCar { (json: NSArray?) in 

     if let results = json { 
      for entry in results { 
       self.items.append(CarObject(json: entry as! Dictionary)) 
      } 
      DispatchQueue.main.async { 
       self.tableView.reloadData() 
      } 
     } 
    } 
} 
func tableView(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
var cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell 
    if cell == nil { 
     cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "TableViewCell") as! TableViewCell 
    } 
    let car = self.items[indexPath.row] 
    print(car.pictureURL) 
    if let url = NSURL(string: car.pictureURL) { 
     if let data = NSData(contentsOf: url as URL) { 
      cell.carImage?.image = UIImage(data: data as Data) 
     } 
    } 

    // Create Swift/Cocoa Touch file with carImage etc. Actionbutton 
    cell.carTitleLabel.text = "test"//car1 
    cell.carPriceLabel.text = car.carPrice 
    print("test3") 
    return cell 
} 

}

Storyboard

app

+0

您是否添加了numberOfRowsInSection委託? –

+0

可能重複[TableView不重新加載](https://stackoverflow.com/questions/16681525/tableview-not-reloading) – Yuyutsu

+0

我試過這些:func numberOfSectionsInTableView(tableView:UITableView) - > Int { return 1 } func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int { return self.items.count; } – Skrai2004

回答

0

您需要設置數據源和代表在您的viewDidLoad() 喜歡的東西下面。

self.tableView.dataSource = dataSource! 
1

像RISHABH說,你需要設置數據源,並委託在viewDidLoad中

override func viewDidLoad { 
    super.viewDidLoad() 
    tableView.datasource = self 
    tableView.delegate = self 
} 
+0

對不起,我忘了在代碼片段中放入「class TableViewController:UITableViewController」這一行。所以這變得多餘。 – Skrai2004

0

你應該有這些功能的默認UITableViewController。也許你錯過了其中之一。

import UIKit 

class YourViewController: UITableViewController { 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 //Your section count 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.items.count //Your row count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell 
    if cell == nil { 
     cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "TableViewCell") as! TableViewCell 
    } 
    let car = self.items[indexPath.row] 
    print(car.pictureURL) 
    if let url = NSURL(string: car.pictureURL) { 
     if let data = NSData(contentsOf: url as URL) { 
      cell.carImage?.image = UIImage(data: data as Data) 
     } 
    } 

    // Create Swift/Cocoa Touch file with carImage etc. Actionbutton 
    cell.carTitleLabel.text = "test"//car1 
    cell.carPriceLabel.text = car.carPrice 
    print("test3") 
    return cell 
} 

} 

注意:您也應該檢查你的self.items.countnumberOfRowsInSection。您的return值不應該爲0.

+0

謝謝,我剛剛嘗試過。它可能已被解決,但我收到另一個錯誤消息:終止應用程序由於未捕獲的異常'NSInternalInconsistencyException',原因:'無法加載捆綁NIB:'NSBundle(已裝載)',名稱爲'TableViewCell'' – Skrai2004

+0

您是否在故事板中設置了TableViewCell的標識符? – kkakkurt

+0

您應打開Storyboard,選擇TableViewCell,在右側菜單中打開Attribute Inspector,找到Table View Cell部分,並在Identifier文本字段中添加「TableViewCell」。 – kkakkurt

相關問題