2017-04-19 146 views
1

我在UITableviewCell中有一個UITablaView。這裏是我的代碼:TableviewCell中的TableView不顯示單元格

的UITableViewCell:

class ProductViewCell: UITableViewCell { 

@IBOutlet weak var totalOrderButton: UIButton! 
@IBOutlet weak var productImage: UIImageView! 
@IBOutlet weak var productNameLabel: UILabel! 
@IBOutlet weak var productSubNameLabel: UILabel! 
@IBOutlet weak var productBioLabel: UILabel! 
@IBOutlet weak var mlButton: UIButton! 
@IBOutlet weak var productQuantity: UILabel! 
@IBOutlet weak var plusButton: UIButton! 
@IBOutlet weak var minusButton: UIButton! 
@IBOutlet weak var greenHeartButton: UIButton! 
@IBOutlet weak var liquorsTableView: UITableView! 

var fourLiquorStores: [LiquorStore] = [] 

var currentUser: User! 

var currentProduct: Product! { 
    didSet { 
     updateUI() 
    } 
} 

func updateUI() { 
    downloadProductImage() 
    productNameLabel.text = currentProduct.name 
    productSubNameLabel.text = currentProduct.subName 
    productBioLabel.text = currentProduct.description 
    productQuantity.text = "\(currentProduct.quantity)" 
    updateGreenHeart() 

} 
var cache = SAMCache.shared() 
weak var delegate: ProductViewCellDelegate! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    liquorsTableView.estimatedRowHeight = 65 
    liquorsTableView.rowHeight = 65 
    liquorsTableView.delegate = self 
    liquorsTableView.dataSource = self 
    fetchLiquorStores() 
} 

func fetchLiquorStores() { 
     LiquorStore.observeNewLiquorStore { (liquorStore) in 
     LiquorStore.observeNewLiquorStore { (liquorStore) in 
      if !self.fourLiquorStores.contains(liquorStore) { 
       self.fourLiquorStores.append(liquorStore) 
       self.delegate.updateTableView() 
       print(self.fourLiquorStores) 
      } 
     } 
    } 
} 




func downloadProductImage() { 
    let productuid = currentProduct.uid 
    let profileImageKey = "\(productuid)" 

    if let image = cache?.object(forKey: profileImageKey) as? UIImage { 
     productImage.image = image 
    } else { 
    currentProduct.downloadPopularProductImage { [weak self] (image, error) in 
     if let error = error { 
      print(error.localizedDescription) 
     } else { 
      self?.productImage.image = image 
      self?.cache?.setObject(image, forKey: profileImageKey) 
     } 
    } 
    } 
} 


// Ad o delete Quantities 
@IBAction func plusDidTap() { 
    if currentProduct.quantity >= 1 && currentProduct.quantity <= 60 { 
     currentProduct.quantity += 1 
     delegate.updateTableView() 

    } 
} 

@IBAction func minusDidTap() { 
    if currentProduct.quantity > 1 { 
     currentProduct.quantity -= 1 
     delegate.updateTableView() 
    } 
} 


func updateGreenHeart() { 
    if let currentUser = currentUser { 
     if currentUser.favoriteProducts.contains(currentProduct) { 
      greenHeartButton.setImage(#imageLiteral(resourceName: "GreenHeartFilled"), for: []) 
     } else { 
      greenHeartButton.setImage(#imageLiteral(resourceName: "GreenHeart"), for: []) 
     } 
    } 
} 

// Ad or delete Favorite Products 
@IBAction func greenHeartDidTap() { 
    if currentUser.favoriteProducts.contains(currentProduct) { 
     self.greenHeartButton.setImage(#imageLiteral(resourceName: "GreenHeart"), for: []) 
     self.currentUser.deleteFavoriteProduct(product: currentProduct) 
    } else { 
     self.greenHeartButton.setImage(#imageLiteral(resourceName: "GreenHeartFilled"), for: []) 
     self.currentUser.addFavoriteProduct(product: currentProduct) 

    } 
} 

}

extension ProductViewCell: UITableViewDataSource, UITableViewDelegate { 


func numberOfSections(in tableView: UITableView) -> Int { 
    return fourLiquorStores.count 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = liquorsTableView.dequeueReusableCell(withIdentifier: "LiquorStoreViewCell", for: indexPath) as! LiquorStoresViewCell 
    cell.currentProduct = self.currentProduct 
    cell.liquorStore = self.fourLiquorStores[indexPath.section] 

    return cell 
} 

}

UITablViewCell -2:

class LiquorStoresViewCell: UITableViewCell { 

@IBOutlet weak var liquorStoreNameLabel: UILabel! 
@IBOutlet weak var liquorStorePriceLabel: UILabel! 
@IBOutlet weak var liquorStoreTimeLabel: UILabel! 
@IBOutlet weak var starStackView: UIStackView! 
@IBOutlet weak var imageViewBoard: UIImageView! 

var currentProduct: Product! 
var currentPriceIndex: Int = 0 

var liquorStore: LiquorStore! { 
    didSet { 
     updateUI() 
    } 
} 



func updateUI() { 
changeLiquorStoreProductsUid() 
liquorStoreNameLabel.text = liquorStore.userName 
let index = liquorStore.products.index(of: currentProduct) 
    if let index = index { 
     let productPrice = liquorStore.products[index].price[currentPriceIndex] 
     format(price: productPrice) 
    } 
} 



// Format Product Price to Currency 
func format(price: Double) { 
    let formatter = NumberFormatter() 
    formatter.locale = Locale.current // Change this to another locale if you want to force a specific locale, otherwise this is redundant as the current locale is the default already 
    formatter.numberStyle = .currency 
    if let formattedTipAmount = formatter.string(from: price as NSNumber) { 
     liquorStorePriceLabel.text = "Tip Amount: \(formattedTipAmount)" 
    } 
} 

// Func to organize LiquorStores 
func changeLiquorStoreProductsUid() { 
    for product in liquorStore.products { 
     if self.currentProduct.name == product.name && self.currentProduct.description == product.description && self.currentProduct.subName == product.subName { 
      let index = liquorStore.products.index(of: product) 
      if let index = index { 
       liquorStore.products[index].uid = currentProduct.uid 
      } 
     } 
    } 
} 



override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
    liquorStorePriceLabel.textColor = UIColor.white 
    liquorStoreNameLabel.textColor = UIColor.white 
    liquorStoreTimeLabel.textColor = UIColor.white 
    imageViewBoard.backgroundColor = #colorLiteral(red: 0.5239839702, green: 0.5239839702, blue: 0.5239839702, alpha: 1) 
    for view in starStackView.subviews { 
     let image = view as! UIImageView 
     image.image = #imageLiteral(resourceName: "WhiteStar") 
    } 

} 

}

UITableviewCell內部的UITableView不顯示數據:/ 問題是什麼?

這是我的建築: StoryBoard

的這種出現在模擬器中,你可以看到,第二的tableview是空的:

Simulator

PD:我從firebase下載成功的酒店。

+0

有截圖嗎?我沒看到一個。 – NRitH

+0

可能的重複http://stackoverflow.com/questions/32586716/tableviewdatasource-and-delegate-not-called-when-tableview-is-inside-collectionv – Luzo

+0

[TableViewDatasource和委託在tableview位於collectionview中時未調用的可能重複細胞](http://stackoverflow.com/questions/32586716/tableviewdatasource-and-delegate-not-called-when-tableview-is-inside-collectionv) – Luzo

回答

0

雖然它可能做你正在嘗試做的,這將是簡單的完全重新考慮你的架構。如果您的視圖控制器僅顯示單個產品,再加上酒類商店列表,那麼產品視圖根本不應該放在單元格中 - 只需將其視爲普通視圖,然後將該視圖作爲表格的標題。這些細胞將成爲酒類商店,您不再需要處理在細胞中嵌套餐桌的問題。或者,可以將產品視圖保留在單元格中,但只需將酒類商店的原型添加爲同一表格中的第二種單元格即可。

編輯:現在你已經發布了你的故事板截圖,最簡單的事情就是將你的酒店原型單元格移動到頂層表格中,從頂層單元格中移除子表格,然後在你的數據源中,有

func tableView(_ tableView: UITableView, cellForRowAtIndexPath path: NSIndexPath) -> UITableViewCell { 
    if path.row == 0 { 
     return tableView.dequeueCellWithIdentifier("productDetailsCell")! 
    } else { 
     return tableView.dequeueCellWithIdentifier("liquorStoreCell")! 
    } 
} 
+0

太棒了!這就是我需要的,非常感謝你! –

+0

很高興幫助! – NRitH

相關問題