【问题标题】:Expand and collapse static table view展开和折叠静态表格视图
【发布时间】:2012-04-05 22:14:28
【问题描述】:

我的应用程序中有一个静态表格视图。此表视图用于首选项。 表格视图中的一个部分只有一个包含UISwitch 的单元格。当这个开关被激活时,我想显示下面的部分,如果不是,我想隐藏下面的部分。所有部分(也应该隐藏/显示的部分)都是使用 Interface Builder 设置的。

当表格视图是静态的,因为静态表格视图没有数据源时,有什么方法可以隐藏或显示此部分?如果更容易,我也可以同意使用相同的部分,但在开关打开或关闭时从该部分添加/隐藏行。

编辑

我已经接近了如何做到这一点。

将节中单元格的高度和节的页脚和页眉的高度设置为0,我可以几乎隐藏节。我在上面的部分和下面的部分之间仍然有一些间距,我无法弄清楚如何摆脱。 有谁知道这个额外的间距是从哪里来的?请看下面的照片。

这是我用来几乎隐藏该部分的代码。

/* Will display cell */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2)
        cell.hidden = YES;
    else
        cell.hidden = NO;
}

/* Height of cell */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2)
        return 0;

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

/* Height of section header */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2)
        return 0;

    return [super tableView:tableView heightForHeaderInSection:section];
}

/* Height of section footer */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2)
        return 0;

    return [super tableView:tableView heightForFooterInSection:section];
}

这就是表格视图现在的样子。还有一些空间我需要隐藏。额外的空格位于标有“Arbejde”和“Anden”的部分之间。

【问题讨论】:

    标签: iphone uitableview static datasource


    【解决方案1】:

    请参阅本教程,这可能会对您有所帮助www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/

    【讨论】:

    • 本教程使用表视图的数据源。我的表格视图是静态的,因此它不使用数据源,但感谢您的回答。
    【解决方案2】:

    我使用问题中的代码让它工作。只需将高度设置为1.0f 而不是0。似乎只有当它的值大于零时高度才有效。

    Reducing the space between sections of the UITableView.

    【讨论】:

    • 您好,您没有回答自己的问题。您添加了如何解决另一个问题。我的问题和你的一样——你是怎么设法让开关扩大细胞的?我遇到了很多困难,您的回答是第一个实际做任何事情的人,但是我无法让它与开关一起使用,它不会扩展。谢谢。
    • 我认为我遇到的问题是我没有使用部分。第二个问题是如何与设置高度的第二种方法进行通信?我可以用第一种方法很好地沟通,但第二种方法对我不起作用。
    【解决方案3】:

    对于静态单元格,我也没有找到合适的答案。如果您希望删除部分或展开单元格,那么这就是我使用的代码。

    在你的 .m 中创建一个 BOOL 值

    @interface yourClassName (){
    
    
    BOOL ddMenuButtonPressed;
    
    }
    
    @end
    

    ddMenu 表示drop down menu

    接下来我在viewDidLoad 方法中将布尔值设置为false。

    ddMenuButtonPressed = false;
    

    然后我初始化 ddButton(在情节提要中控制 + 从您的按钮拖动或切换到您的 .h 文件并创建操作并将名称设置为 ddMenuButton)并在我的 .m 文件中使用此方法。

    - (IBAction)ddMenuShow:(UIButton *)sender
    {
         if (sender.tag == 0) {    
            sender.tag = 1;
            ddMenuButtonPressed = true;
    
        } else {
            sender.tag = 0;
            ddMenuButtonPressed = false;
    
        }
        //very important that you include these
        //they update the view like a viewDidLoad statement without leaving the screen
        //if you have any data entry points in your cell like a textfield it **will not** erase that data fortunately
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    
    }
    

    最后我们添加如下方法:

    // Handle expanding and minimising cell or section
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //if the section is 0 (and there is only one cell) and ddMenuButtonPressed hasn't been pressed
        if (indexPath.section == 0 && ddMenuButtonPressed == false){
    
        //return the height you have it set as in story board (or a number)
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    
        }
        //if the section is 0 and the ddMenuButton has been pressed
        if (indexPath.section == 0 && ddMenuButtonPressed == true){
        //change the cell height to 380 or whatever size you want
        return 380;
        }
        //otherwise leave cells as they are
        else {
    
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    
        }
    }
    

    就是这样。没有花哨的编码,漂亮干净(减去评论)和简单,你可以一遍又一遍地使用它来做你喜欢的多个部分。

    【讨论】:

      【解决方案4】:

      对于静态单元格,我只使用这个:

      override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cell = self.tableView(self.tableView, cellForRowAt: indexPath)
        if cell.isHidden {
          return 0
        } else {
          return cell.bounds.size.height
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-08-23
        • 2018-07-31
        • 2020-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        • 2019-03-12
        相关资源
        最近更新 更多