展开并折叠tableview行

时间:2016-04-30 05:06:56

标签: ios tableview

我正在开发一个iOS应用程序。我有一个可扩展和折叠的tableview。单击tableview的单元格,tableview单元格正确展开,但是当我单击第二个单元格时,我希望第一个单元格折叠,第二个单元格展开。这样一次只会扩展一个单元格。

我正在使用HVTableview类: - 代码: -

@Override
public void run() {
    try{
        LOGGER.info("Now begin a new round of refreshing deployment data...");
        this.doWorkFlow();
    }catch(Exception e){
        return;
    }

}

1 个答案:

答案 0 :(得分:0)

- (void) collapseExpandButtonTap:(id) sender
{
    UIButton* aButton = (UIButton*)sender; //It's actually a button
    NSIndexPath* aPath = [self indexPathForCellWithButtonTag:aButton.tag]; //Let's assume that you have only one section and button tags directly correspond to rows of your cells.
    //expandedCells is a mutable set declared in your interface section or private class extensiont
    if ([expandedCells containsObject:aPath])
    {
        [expandedCells removeObject:aPath];
    }
    else
    {
        [expandedCells addObject:aPath];
    }
    [myTableView beginEditing];
    [myTableView endEditing]; //Yeah, that old trick to animate cell expand/collapse
}

在UITableViewDelegate方法中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([expandedCells containsObject:indexPath])
    {
        return kExpandedCellHeight; //It's not necessary a constant, though
    }
    else
    {
        return kNormalCellHeigh; //Again not necessary a constant
    }
}

这里的关键是确定您的单元格是否应该展开/折叠并在委托方法中返回正确的高度。因此,请相应地设计您的要求。