Reputation: 1582
This is killing me!
I have a UITableView
with cells that has an image, name and phone number in them.
Look here:
Now, I want to expend the cell's height when touching it so I can add some buttons in there. When I expend the cell, I also change it's bg image (to a higher one).
I do it using [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
and it works nice, animates the cell down smoothly, except that it also changes the cell's content:
Anything I can do so the content will stay as it was after cell resizing?
BTW, i did try using,
[_tableView beginUpdates];
[_tableView endUpdates];
but the cell just "jumped" to it's new height so I gave it up.
Upvotes: 3
Views: 1134
Reputation: 230
Try following delegation method to set height of tableview
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0;
}
You can create an array of images
imgPaths = [[NSArray alloc]
initWithObjects:
[[NSBundle mainBundle] pathForResource:@"abc" ofType:@"png"],
[[NSBundle mainBundle] pathForResource:@"def" ofType:@"png"],nil];
Add it in cellForRowAtIndexPath methods.
example:
[cell.imageView setImage:[UIImage imageWithContentsOfFile:[imgPaths objectAtIndex:indexPath.row]]];
Upvotes: 0
Reputation: 1582
Thank you all for the answers, but nothing helped.
At first I used the built-in cell items (imageView, textLabel, detailTextLabel) and these are changing sizes as the cell transitions.
When I added my custom subviews, with defined frames, to the cell it worked.
Upvotes: 0
Reputation: 9977
You need to be aware of the layouting of the contentView.
If you use Autolayout, check your constraints (cell.contentView.constraints
). If you use UIViewAutoresizing, set correct autoresizingMasks (cell.contentView.autoresizingMask
) for all subviews of contentView. You could also try to turn off autoResizing (cell.contentView.autoresizesSubviews = NO
), but this will likely result in wrong results.
Also I'm wondering, why there is an arrow in the cell backgroundImage? Probably you should use other images too.
Upvotes: 2
Reputation: 534
You can create two custom cells one before expansion and another one for expanded cell.
when you touch the cell store the selected cell indexpath
and while reloading the table, in cellForRowAtIndexPath:
check the indexpath
and load cell accordingly.
Upvotes: 2
Reputation: 2595
Try setting UIViewAutoresizing
mask of name and phone number labels (or any other UI elements in the content) to fixed top margin.
Upvotes: 1