单击时调整tableView全屏大小

时间:2016-11-21 18:44:43

标签: ios iphone swift uitableview uiimageview

我正在研究以下内容: Screenshot

基本上它是顶部另一个UIImageView内部的一个UIImageView(屏幕的60%)和一个占据屏幕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

2 个答案:

答案 0 :(得分:2)

除了使用新的高度重新加载单元格外,还需要更改表格视图的框架。如果你想使表占据整个屏幕,请确保为它创建一个IBOutlet,比如myTableView,然后,在:

之后
tableView.reloadRows(at: [indexPath], with: .automatic)

添加此代码:

myTableView.frame = view.bounds

您还可以创建一个Bool变量来跟踪表视图的正常和全屏模式,以及一些CGRect变量来保持初始帧。当您希望表视图返回其初始帧时,您可能需要这样的东西:

myTableView.frame = initialTableViewFrame

希望你明白这一点。

答案 1 :(得分:0)

这是一种纯粹的自动布局方法:

1.在界面构建器中,创建一个带有约束的UIImageView [尾随,前导,顶部,底部,高度],使高度> =并且优先级为999.稍后您将在代码中使用此高度用于展开/折叠逻辑。 Elements Layouts

2.创建一个虚拟UIView,其高度将在以后以编程方式设置,当您为imageview创建扩展效果时,此UIView将“推”/“拉伸”您的单元格高度。将其可见性设置为隐藏,因为您不需要向用户显示此内容。 Elemets Layouts

3.在自定义单元格中,创建两个NSLayoutConstraint实例作为@IBoutlets,一个用于UIImageView高度,一个用于虚拟UIView高度,然后在接口文件中将它们连接起来。

let lowPriorityHeight : Float = 250;
let highPriorityHeight : Float = 999;
class CustomCell: UITableViewCell {
    @IBOutlet weak var imgVwHeight: NSLayoutConstraint!
    @IBOutlet weak var tempHeight : NSLayoutConstraint!
    var isExpanded = false{
        didSet{
            imgVwHeight.priority = (isExpanded) ? lowPriorityHeight : highPriorityHeight;
            if isExpanded == true{
                tempHeight.constant = ExpandedHeight
            }else{
                tempHeight.constant = NormalHeight
            }
        }
    }

4.在您的主课程中,将isExpanded设置为true或false以创建效果。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! CustomCell
        cell.isExpanded = !cell.isExpanded
        tableView.reloadData()
    }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "kCustomCell", for: indexPath) as! CustomCell
        return cell
    }

您可以下载我的示例实现HERE

相关问题