在問題標題中提到的場景中,在更改段時,理想情況下UITableView
應該重新加載,因此UITableViewCell
也應該重新加載。問題是,所有內容都像標籤文本一樣更新。但是,如果我在一個細分受衆羣中擴展了細胞的子視圖,那麼在細分受衆羣發生變化後,它仍然會展開。
段索引變更功能:帶UITableView的UISegmentControl - UITableViewCell佈局在更改段時保持不變
@IBAction func segmentOnChange(sender: UISegmentControl)
{
// Few lines
// self.tableMenu.reloadData()
}
截圖1:
截圖2:
所以理想情況下,在截圖2中,購物車視圖應該已被摺疊。
更新:
顯示/隱藏視圖:
func showHideCartView(sender: UIButton)
{
let cell = self.tableMenu.cellForRow(at: IndexPath(row: 0, section: Int(sender.accessibilityHint!)!)) as! RestaurantMenuItemCell
if sender.tag == 1
{
// Show cart view
cell.buttonArrow.tag = 2
cell.viewAddToCart.isHidden = false
cell.constraint_Height_viewAddToCart.constant = 50
cell.buttonArrow.setImage(UIImage(named: "arrowUp.png"), for: .normal)
}
else
{
// Show cart view
cell.buttonArrow.tag = 1
cell.viewAddToCart.isHidden = true
cell.constraint_Height_viewAddToCart.constant = 0
cell.buttonArrow.setImage(UIImage(named: "arrowDown.png"), for: .normal)
}
self.tableMenu.reloadData()
}
的cellForRowAtIndexPath:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RestaurantMenuItemCell", for: indexPath) as! RestaurantMenuItemCell
cell.selectionStyle = .none
let menuItem = self.menuItems[indexPath.section]
cell.imageViewMenuItem.image = UIImage(named: "[email protected]")
cell.labelName.text = menuItem.name
cell.labelDescription.text = menuItem.description
cell.labelPrice.text = String(format: "$%i", menuItem.price!)
cell.buttonArrow.accessibilityHint = String(format: "%i", indexPath.section)
cell.buttonArrow.addTarget(self, action: #selector(self.showHideCartView(sender:)), for: .touchUpInside)
return cell
}
如何擴展表格單元格?意味着在heightForRowAtIndexPath方法中管理? – iPatel
這應該有一個簡單的解決方法..檢查你的模型,這決定了一個單元格是否擴展或不..你必須重置它的段更改 –
@iPatel:通過約束。我已經設置estimatedRowHeight = 150和rowHeight = UITableViewAutomaticDimension – Nitish