关于Objective C:在UITableViewCell中选择后展开和折叠特定行

Expand and Collapse specific row after selected in UITableViewCell

我正在尝试实现展开/折叠表格视图单元格。它对许多行都有效。单击后可以展开和折叠它们。

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

这里是我的代码:

1
2
3
4
5
6
7
8
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectedIndex ==  indexPath.row){
        return 100;
    }else{
        return 44;
    }
}

//在单元格中显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- (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;
}

//所选单元格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-(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];
}

//我想显示我的屏幕截图。
enter image description here

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

感谢您的阅读。


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