【发布时间】:2017-10-05 20:45:44
【问题描述】:
在问题标题中提到的场景中,在更改段时,理想情况下 UITableView 应该重新加载,因此 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: "recommend0@2x.png")
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 :通过约束。我设置了estimatedRowHeight = 150 和rowHeight = UITableViewAutomaticDimension
-
@MilanNosáľ:用代码更新了我的问题
-
@Nitish 检查我的答案
标签: ios swift uitableview uisegmentedcontrol