Resizing UITableViewCell frame

I am trying to resize the border UITableViewCell's


 [cell setFrame:CGRectMake(cell.frame.origin.x, 


however, it doesn't resize after I do this ... why is that?

It's weird as if I add to the cell UIToolBar

it changes, but when I add the UIView it doesn't:

[cell.contentView addSubview:sideSwipeView];



source to share

2 answers

Here's the long and short:

  • The width of your cell is determined by the width of the table it is in.

[EDIT: If it is a grouped table view, the cell is 20-60 pixels narrower than the table width, depending on whether you are using an iPhone or iPad.]

  1. The height of your cell is determined by the method heightForRowAtIndexPath


If you manually set the cell's frame it will be useless unless you are using a subclassed cell where you want to add subviews based on the cell sizes.

Even so, it rectForRowAtIndexPath:(NSIndexPath*)indexPath

is a good idea to get the cell frame from the table view using the method and then set that frame as the cell frame (after setting the Y origin as 0).

I'm not entirely sure about the UIToolBar, but your subframe frame won't change when the cell frame changes.

Maybe if you could tell us what you are trying to achieve, we can offer you a solution?

-------------------- EDIT --------------------

So you need to dynamically add the subitem to the cell when you click on it and change its height in accordance with the new subview. It will look hairy, like this:

In your .h file, declare:

BOOL subviewAdded;


In your .m file, in init, do:

subviewAdded = NO;


Let's say you want the cell height to be 50 without subview and 100 with subview. Accordingly, your heightForRow method should be:

- (CGFloat)tableView:(UITableView *)tableView 
           heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return (subviewAdded?100.0f:50.0f);


This means that initially, since subviewAdded is NO, all your cells will have a lower height.

Now, to add a subview to a cell when you click on it and dynamically change its height, do this in your didSelectRow method:

- (void)tableView:(UITableView *)tableView 
        didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Get the cell at this indexPath

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];

        subviewAdded = NO;

        for(int i = 0; i < [thisCell.contentView.subviews count]; i++)
            UIView *thisSubview = [thisCell.contentView.subviews objectAtIndex:i];
            [thisSubview removeFromSuperview];
        UIView *someView = [[UIView alloc] initWithFrame:someFrame];

        [thisCell.contentView addSubview:someView];
        [someView release];

        subviewAdded = YES;

    NSMutableArray *array = [NSMutableArray array]; 

    [array addObject:indexPath];

    [tableView reloadRowsAtIndexPaths:array 


So what happens here is you add a subview to this cell that you used. Reloading this cell will trigger heightForRowAtIndexPath

and do a little fade out animation and change the height of the table.

IMPORTANT: Ideally, you should maintain an array of NSNumbers with boolean values. The size of the array must be the same size as the number of table cells.

In heightForRow you should check this array instead of using one boolean for the whole tableView. This ensures that you can have different heights for different cells.

It looks something like this:

- (CGFloat)tableView:(UITableView *)tableView 
           heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL thisBool = (BOOL)[[booleanArray objectAtIndex:indexPath.row] boolValue];

    return (thisBool?100.0f:50.0f);


I haven't posted all of this code here as it was intended, and what I have posted should put you well on the road to boolean array maintenance.

Anyway, that's all. I just tested this code myself to make it work :)



If you want to increase the height of your cell based on some parameter eg. text, image, you have to implement the heightForRowAtIndexPath

method UITableViewDelegate

in your code.

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




All Articles