Increase table row height dynamically

305 views Asked by At

I have a table with one custom cell with one label and one textfield.Am re-using the same cell with some 5 rows. I want to display one view beneath my textfield only if user enters on the textfield. When am entering data in first textfield view should be hidden for other rows and respectively for all other rows. Default height must be 120 and when am displaying my view row height should increased to 250. Here is my code,

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

      return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"DetailsTableCell";
cell = (NewsDetailsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[NewsDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.row == 0) {
    cell.newView.tag = 200;
}
else if (indexPath.row == 1) {
    cell.newView.tag = 201;
}
else {
    cell.newView.tag = 202;
}

   return cell;
}

Here new view is my UIView which am displaying on text change.

- (IBAction)textFieldChanged:(UITextField *)textField replacementString:(NSString *)string {

NSIndexPath *indexPath = [newsTable indexPathForCell:(NewsDetailsTableViewCell*)[[textField superview] superview]];
NSIndexPath *myPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
NewsDetailsTableViewCell *newsTableCell = (NewsDetailsTableViewCell*)[newsTable cellForRowAtIndexPath:myPath];

 if (indexPath.row == 0 && newsTableCell.newView.tag == 100) {
    newsTable.rowHeight = 250;
    newsTableCell.newView.hidden = false;

} else if (indexPath.row == 1 && newsTableCell.newView.tag.tag == 101) {
    newsTable.rowHeight = 250;
    newsTableCell.newView.hidden = false;
} else {
    newsTable.rowHeight = 250;
    newsTableCell.newView.hidden = false;
   }
}

Am getting my view in my text change but row height is not getting changed. Also It still showing 120 height.

Also I tried this,

NSArray* rowsTobeReloaded = [NSArray arrayWithObjects:indexPath, nil];
[newsTable reloadRowsAtIndexPaths:rowsTobeReloaded withRowAnimation:UITableViewRowAnimationNone];

But it didn't work. Should I re-load my tableview again in text change? or am I missing something?

2

There are 2 answers

0
KavyaKavita On

YOu need to change height in

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


  if (usingNewView){
     return 250;
  }
  return 120;
}

And set logic for usingNewView in

- (IBAction)textFieldChanged:(UITextField *)textField replacementString:(NSString *)string
0
Rohit Agarwalla On

You have to modify the logic in the below method:enter image description here

The selectedRowNum can be modified as per the row in which the textField is to be shown. Also, cellForRowAtIndexPath is not used for setting the height of rows in a tableview. Everytime, we call the reloadData or reloadIndexes the heightForRowAtIndexPath method is called.