我正在做以下工作: Screenshot
基本上它是一个位于顶部(60% 屏幕)的另一个 UIImageView 内部的 UIImageView 和一个占据 40% 屏幕的 UITableView
和代码(见下文):
我想要实现的是在单击我的 customCell 时调整 UITableView 单元格的全屏大小,并在未单击时将其缩小到原始状态。
有什么办法可以实现吗? 提前致谢。
//Label outlets
@IBOutlet weak var tableView: UITableView!
var selectedIndexPath : IndexPath? = nil
override func viewDidLoad() {
super.viewDidLoad()
//MARK Start of CustomViewCell Code
tableView.register(UINib(nibName: "CustomViewCell", bundle: nil), forCellReuseIdentifier: "Cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomViewCell
cell.clipsToBounds = true
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let index = indexPath
if selectedIndexPath != nil {
if index == selectedIndexPath {
return 667
} else {
return 62
}
} else {
return 62}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch selectedIndexPath {
case nil:
selectedIndexPath = indexPath
default:
if selectedIndexPath! == indexPath {
selectedIndexPath = nil
} else {
selectedIndexPath = indexPath
}
}
tableView.reloadRows(at: [indexPath], with: .automatic)
}
//MARK End of CustomViewCell Code
最佳答案
除了重新加载具有新高度的单元格外,您还需要更改表格 View 的框架。如果你想让表格占据整个屏幕,请确保为它创建一个 IBOutlet,比如 myTableView,然后,之后:
tableView.reloadRows(at: [indexPath], with: .automatic)
添加这段代码:
myTableView.frame = view.bounds
您还可以创建一个 Bool 变量来跟踪 TableView 的正常和全屏模式,以及一些 CGRect 变量来保持初始帧。当您希望 TableView 返回到其初始框架时,您可能需要这样的东西:
myTableView.frame = initialTableViewFrame
希望你明白了。
关于ios - 单击时调整 tableView 全屏大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727264/