ios - 单击时调整 tableView 全屏大小

标签 ios iphone swift uitableview uiimageview

我正在做以下工作: 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/

相关文章:

ios - 如何在ios中选择ipad应用程序?

iOS - AVFoundation - 是否可以将整个视频设置为慢动作?

ios - 如何在 Kotlin/Native iOS 开发中通过 HTTP 连接?

ios - Firebase 循环多级节点

ios - 带参数的 Alamofire 4 Swift 3 GET 请求

ios - Swift - 添加到数组后 tableView 不重新加载

ios - 根据 iPhone 收件箱中的文本执行某些操作

iphone - 从 ViewController 返回 View

ios - 如何设置 Scrollview 的 ContentOffset 当我滑动图像时,我将其图像偏移量设置到 iOS 中的 Scrollview 中?

swift - 如何更改导航栏标题位置?