I'm expanding my cell via:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedRow && indexPath.row == selectedRow.row) {
tableView.scrollEnabled = NO;
return 480;
}
return 250;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath;
[tableView beginUpdates];
[tableView endUpdates];
CGFloat height = 250.0f;//cell height
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:YES];
}
It works fine, whenever I select a cell, it do expands, but I couldn't figure out how to bring it back to its original height. Any thoughts?
Try This :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedRow.row == indexPath.row)
selectedRow = nil;
else
selectedRow = indexPath;
[tableView beginUpdates];
[tableView endUpdates];
[tableView reloadData];
}
Try this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedRow && indexPath.row == selectedRow.row) {
tableView.scrollEnabled = NO;
return 480;
}
tableView.scrollEnabled = YES;
return 250;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 250.0f;
if (selectedRow && indexPath.row == selectedRow.row) {
NSIndexPath *index = [NSIndexPath indexPathForRow:selectedRow.row inSection:selectedRow.section];
selectedRow = nil;
[tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
[tableView setContentOffset:CGPointMake(0, 0 )animated:NO];
} else {
selectedRow = indexPath;
[tableView beginUpdates];
[tableView endUpdates];
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:NO];
}
}
I would suggest you to add a custom property like isExpanded
in your tableView datasource object and switch that property based on selection and return height according to this value.
For ex: In your heightForRowAtIndexPath
method, you could have somthing like this.
if(yourCustomCell.isExpanded == true){
return 480;
}
else{
return 250;
}
In didSelectRowAtIndexPath
method, just set or unset this property.
yourCustomCell.isExpanded = !yourCustomCell.isExpanded
[tableView reloadData]; //Which will call heightForRow and then assign new height based on the property value.
Now if you dont have a custom cell object, then I think you can make use of cell's isSelected
property.
I haven't tried these, but I guess this should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.