繁体   English   中英

如何在swift中隐藏静态UItableview中的特定单元格?

[英]How to hide specific cells in a static UItableview, in swift?

没有文章清楚地解释我的查询,我在静态表中有三个单元格,我想在用户点击第一个单元格时隐藏第二个单元格。 任何形式的帮助表示赞赏。

虽然您无法阻止静态表尝试显示单元格,但您可以将其高度设置为零,使它们实际上不可见:

将此方法添加到表视图控制器委托类:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
    return cell == myHiddenCell ? 0 :  super.tableView(tableView, heightForRowAtIndexPath:indexPath)
}

didSelectCellAtIndexPath方法中,您可以将高度设置为0以隐藏它:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 0 {
        let indexPath = NSIndexPath(forItem: 1, inSection : 0)
        let secondCell = tableview.cellForRowAtIndexPath(indexPath)
        secondCell.frame.size.height = 0;
        self.view.layoutSubviews()
    }
}

如果你想要一个动画,只需将self.view.layoutSubviews()放在UIView动画方法UIView.animateWithDuration ......等中

对我来说,将某些单元格的高度设置为0而其他单元格的高度不是一个选项,因为我的所有单元格都有不同的高度。

我在Storyboard中创建了另一个单元格,并将行高设置为0(在大小检查器中)。 然后在代码中,如果我想要隐藏它,我会显示高度为0的单元格,如果没有,我会显示另一个单元格:

if (hideCell) {
            let hiddenCell = tableView.dequeueReusableCell(withIdentifier: "hiddenCell",for: indexPath) as! TheWallTableViewCell
            return hiddenCell
        }
else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) as! TheWallTableViewCell
        return cell 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM