I am trying to resize my UITableViewCell's frame via:
[cell setFrame:CGRectMake(cell.frame.origin.x,
cell.frame.origin.y,
cell.frame.size.width,
cell.frame.size.height+25)];
however, it’s not resizing after I do this… why is this?
This is weird as if I add a UIToolBar into the cell, it resizes but when I am adding a UIView, it doesn’t:
[cell.contentView addSubview:sideSwipeView];
Here’s the long and short of it:
[EDIT: If it’s a grouped table view, the cell is 20 – 60 pixels narrower than the tableview width, depending if you’re using an iPhone, or an iPad.]
heightForRowAtIndexPathmethod.If you’re manually setting the cell’s frame, it’s going to be useless except when you’re using a subclassed cell where you want to add subviews based on the cell’s dimensions.
Even in this case, it’s recommended to get the cell’s frame from the tableview by using
rectForRowAtIndexPath:(NSIndexPath*)indexPathmethod and then setting that frame as the cell’s frame (after setting the frame’s origin Y as 0).I’m not quite sure about the UIToolBar, but your subview’s frame won’t change on changing the cell frame.
Maybe if you could tell us what you’re trying to achieve, we can suggest a solution for you?
——————–EDIT——————–
So you need to dynamically add a subview to a cell on tapping it and resize it’s height according to the new subview. This is gonna get hairy so here goes:
In your .h file declare:
In your .m file, in the init, do:
Let’s assume that you want the cell’s height to be 50 without the subview and 100 with the subview. Accordingly, your heightForRow method should be:
This means that initially since subviewAdded is NO, all your cells will have the smaller height.
Now, to add a subview to a cell on tapping it, and to change it’s height dynamically, do this in your didSelectRow method:
So what’s going to happen here is you’re adding a subview to this cell you’ve tapped. Reloading this cell will call
heightForRowAtIndexPathand do a nice little fade animation and change your tableview heights.IMPORTANT: Ideally, you should maintain an array of NSNumbers with boolean values. The array size should be the same size as the number of tableview cells you have.
In heightForRow, you would then check against this array instead of using a single boolean for the whole tableView. This would ensure that you could have different heights for different cells.
That would look something like:
I didn’t post all that code here since it’s implied and what I’ve posted should put you well on your way to doing the boolean array thing.
Anyway, there you are. I just tested this code myself so it works 🙂