I have a UITableView
that has a number of static cells. There are no prototypes.
One cell as a UITextField
and a UITextView
. I want the UITextView
to be hidden depending on the input of the UITextField
. This is not a problem.
My problem is that I would like to resize the UITableViewCell
that both of these controls are in - so that when the UITextView
is hidden the cell height is 30, and when it is displayed its height is 70 (this makes the UITextView
fully visible).
As they are static cells, I have simply set the height in IB to 70 (the largest) so when the UITextView
is hidden there is a lot of unused space in the cell which I would like to get rid of by changing the cell height to 30.
I assume that I would need to tell the cell to redraw using something like
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
But how do I get the reference to the cell from a subview (the UITextField
)?
Also how would I tell the cell the new height? Should I tag it and use
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
as I would for dynamic prototype sizing?
You can use a little trick. Create 2 iVars
1) NSIndexPath *selectedIndex;
2) BOOL isTextViewFull;
While toggling the Hide/Show textView
if(showfulltextview)
{
selectedIndex = indexPath; // IndexPath of the current cell selected
isTextViewFull = YES;
}
else
{
selectedIndex = nil;
isTextViewFull = NO;
}
[_yourTableView reloaddata];
Now in your delegate you can use those iVars
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 30;
if(isTextViewFull && indexPath.row == selectedIndex.row)
{
height = 70;
}
return height;
}
Here is a way to get your IndexPath for the TextField if you don't have the track of it. While editing the Textfield inside the Textfield Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// Get the cell in which the textfield is embedded
id textFieldSuper = textField;
while (![textFieldSuper isKindOfClass:[UITableViewCell class]]) {
textFieldSuper = [textFieldSuper superview];
}
NSIndexPath *indexPath = [_yourTableView indexPathForCell:(UITableViewCell *)textFieldSuper];
}
When it comes to dynamically changing the height of the tableviewcells.
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return Yourvalue; //return whatever u want ur cell to have size.
}
use this method and return the value that u think ur cell should have, for instance, if ur label inside the cell is also created dynamically then, return the size of the label as per u created.
Hope this helps.
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.