开发者

resizing a UITableViewCell frame issue

开发者 https://www.devze.com 2023-03-05 14:14 出处:网络
I am trying to resize my UITableViewCell\'s frame via: [cell set开发者_如何学PythonFrame:CGRectMake(cell.frame.origin.x,

I am trying to resize my UITableViewCell's frame via:

 [cell set开发者_如何学PythonFrame: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:

  1. Your cell width is determined by the width of the tableview it's in.

[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.]

  1. Your cell height is determined by the heightForRowAtIndexPath method.

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*)indexPath method 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:

BOOL subviewAdded;

In your .m file, in the init, do:

subviewAdded = NO;

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:

- (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 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:

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

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];

    if(subviewAdded)
    {
        subviewAdded = NO;

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

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

        subviewAdded = YES;
    }

    NSMutableArray *array = [NSMutableArray array]; 

    [array addObject:indexPath];

    [tableView reloadRowsAtIndexPaths:array 
                     withRowAnimation:UITableViewRowAnimationFade];
}

So what's going to happen here is you're adding a subview to this cell you've tapped. Reloading this cell will call heightForRowAtIndexPath and 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:

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

    return (thisBool?100.0f:50.0f);
}

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 :)


If you want to increase the height of your cell based on some parameter eg. text, image, you must implement the heightForRowAtIndexPath method of UITableViewDelegate in your code.

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

精彩评论

暂无评论...
验证码 换一张
取 消