我幾乎是Swift編程中的新手。我正在使用Swift 3和最新版本的Xcode,我試圖做一些對你們來說可能很容易的事情,所以在這裏:無法生成動態UITableView
我想動態填充一個TableView從Firebase數據庫中檢索數據。爲此,我設計了一個Prototype單元,其中包含一個UIImageView,一些UILabels和一個步進器。我認爲Stepper和其中一個UILabel不需要在生成時進行編輯。但我需要放置圖片並編輯2個UILabels。以下是我正在嘗試這樣做:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Set cell contents
let cell = tableView.dequeueReusableCell(withIdentifier: cellId)
let imageView = cell?.viewWithTag(0) as! UIImageView
var price:UILabel = cell?.viewWithTag(2) as! UILabel
var name:UILabel = cell?.viewWithTag(1) as! UILabel
let amount = cell?.viewWithTag(3) as! UILabel
name.text = productList[indexPath.row].name
price.text = "" + (productList[indexPath.row].price?.description)!
let imageDict:NSDictionary = productList[indexPath.row].imageName!
let image = downloadImageProductFromFirebase(append: imageDict["hash"] as! String)
imageView.image = image
return cell!
}
當我運行應用程序,這是我發現的錯誤:
無法投類型的值「UITableViewCellContentView」(0x10442bff8)到「的UIImageView '(0x10441c738)。
但是有些東西很好,我知道從Firebase中正確地檢索數據,導致它在生成TableView單元格之前通過控制檯進行打印。
在此先感謝,夥計們!
'讓的ImageView =細胞.viewWithTag(0)作爲! UIImageView'找到單元格的contentView,因爲'0'是視圖的默認標籤值。爲你的'UIImageView'使用另一個標籤值。 – vacawama