Is it possible to change the height of the current row in a UITableView?

Very simple question ...

can I change the height of only the current row (click on a row) in a UItableView?

Note. I do not want to influence the size of the remaining lines.

+4
source share
4 answers

Yes, you can. You need a variable containing the last selected row. For instance:

@property (nonatomic, assign) NSInteger selectedRow; 

... Then we implement the method

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

and then update the method:

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if(self.selectedRow == indexPath.row) self.selectedRow = -1; else self.selectedRow = indexPath.row; //The magic that will call height for row and animate the change in the height [tableView beginUpdates]; [tableView endUpdates]; } 

NOTE Initialize your self.selectedRow to -1 at the beginning of default 0 .

+7
source

You can use heightForRowAtIndexPath to set the height for the row

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

You can put a check on NSIndexPath and return height for that particular indexpath

+1
source
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row == rowtochangeheight) { return 60; } else { return 44; } } 
0
source

Yes, you can do this, but you need to keep the index path for the selected row, and you must initialize the selected Row variable with a negative value.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectedRow = indexpath.row; [tblView reloadData]; } - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == selectedRow) return 150.0f; return 30; } 
0
source

Source: https://habr.com/ru/post/1480453/


All Articles