0

I am trying to implement the expand/collapse table view cell. It's working good for many rows; they can be expanded and collapsed after clicked.

But what happen is when my table view has only one row; when I click it and then all the rest of the rows are expended too even though they are empty.

Here my code:

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

// Display in cell

- (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;
}

// Selected 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];
}

// I would like show my screen shot. enter image description here

How to expand only selected row when there is only one result after search? How to keep the other empty stay the same !

Thank for reading.

Mab KaaKoo
  • 125
  • 1
  • 6
  • 19

1 Answers1

1

I don't think you can change that behavior for a one row table -- the table shows the cell dividers based on the height of that one row. If you don't want this look, then I think you have to set the separatorStyle to UITableViewCellSeparatorStyleNone. You could make a custom cell that has a line at the bottom to mimic a cell separator if want to see a line only at the bottom of filled cells.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • I have tried that already. It's doesn't help. my search result tableview contain only one row, when I click on that row it expanded good but the rest of other rows are expanded too. How to solve this problem? Thank – Mab KaaKoo Dec 22 '13 at 07:29
  • @MabKaaKoo, what did you try? If you do self.strongSearchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;, you should not see any separators, no matter how many rows there are. Are you saying you did that, and you still see row separators? – rdelmar Dec 22 '13 at 17:31