【问题标题】:Expand and Collapse specific row after selected in UITableViewCell在 UITableViewCell 中选择后展开和折叠特定行
【发布时间】:2013-12-21 16:36:26
【问题描述】:

我正在尝试实现展开/折叠表格视图单元格。它适用于多行;点击后可以展开和折叠。

但是当我的表格视图只有一行时会发生什么;当我单击它时,即使它们是空的,所有其余的行也会被消耗掉。

这是我的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectedIndex ==  indexPath.row){
        return 100;
    }else{
        return 44;
    }
}

//在单元格中显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"CommentTableCell";
    //-- try to get a reusable cell --
    CommentTableCell *cell = (CommentTableCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //-- create new cell if no reusable cell is available --
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    VocabularyController *vc;

    // Display word from database else display vocabulary when searching
    if (tableView != self.strongSearchDisplayController.searchResultsTableView) {
        vc = [self.myArray objectAtIndex:indexPath.row];
    }else{
        vc = [self.filteredArray objectAtIndex:indexPath.row];
    }
    cell.nameLabel.text = vc.korean;

    return cell;
}

// 选中的单元格

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(selectedIndex == indexPath.row){
        selectedIndex = -1;

        NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];

        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
        return;
    }

    if(selectedIndex >= 0){
        NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:selectedSection];
        selectedIndex = indexPath.row;

        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    // Finally set the selected index to the new selection and reload it to expand
    selectedIndex = indexPath.row;

    [tableView beginUpdates];
    [tableView endUpdates];
}

// 我想显示我的屏幕截图。

搜索后只有一个结果时如何仅展开选定的行?如何让其他空保持不变!

感谢阅读。

【问题讨论】:

    标签: objective-c uitableview


    【解决方案1】:

    我认为您无法更改单行表的这种行为——该表根据该行的高度显示单元格分隔符。如果你不想要这种外观,那么我认为你必须将 separatorStyle 设置为 UITableViewCellSeparatorStyleNone。如果只想在填充单元格的底部看到一条线,您可以制作一个底部有一条线的自定义单元格来模拟单元格分隔符。

    【讨论】:

    • 我已经尝试过了。它没有帮助。我的搜索结果 tableview 仅包含一行,当我单击该行时,它展开得很好,但其他行的其余部分也展开了。如何解决这个问题呢?谢谢
    • @MabKaaKoo,你尝试了什么?如果您执行 self.strongSearchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;,则无论有多少行,您都不应该看到任何分隔符。你是说你这样做了,但你仍然看到行分隔符?
    • @MabKaaKoo,如果它解决了你的问题,你应该接受答案。
    猜你喜欢
    • 2011-07-08
    • 2017-01-11
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2016-08-25
    • 2013-04-16
    相关资源
    最近更新 更多