Reputation: 125
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.
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.
Upvotes: 0
Views: 1430
Reputation: 104082
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.
Upvotes: 1