質問

I´m using custom cells in a TableView.

The cell height is calculated based on an NSString that is loaded in a UILabel of the cell. The size is calculated using this function

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [self getTableViewRow:tableView index:indexPath];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + (CELL_CONTENT_MARGIN * 2) + 60;

}

The size is calculated correctly but when the cell is loaded to the uiTableView, the row has the correct height but the cell does not have.

This is where i create my cell

//Com Custom Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellApresentacao" owner:self options:nil];

    if ([nib count] > 0 )
        cell = self.tvCell;    
    else
        NSLog(@"Falhou a carregar o xib");
}    

NSInteger row = [indexPath row];

if(self.myTableView1 == tableView)
    labelDescricaoApresentacao.text = [listData1 objectAtIndex:row];
else if (self.myTableView2 == tableView) 
    labelDescricaoApresentacao.text = [listData2 objectAtIndex:row];
else
    labelDescricaoApresentacao.text = [listData3 objectAtIndex:row];  

return cell;
}

i tried to change the cell height in this method using

cell.frame.size.height

but it still didn´t loaded the correct heigth.

Do i have to do anything in the xib of the customCell? How should i update the cell heigth to the same size has the row?

役に立ちましたか?

解決

Where did you set the cell.frame.size.height? I would recommend trying to put it here:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.frame = CGRectMake(0,0,320.0f,30.0f);
}

This method gets called right before the cell is displayed and is a good place to make any visual changes to the cell.

他のヒント

If you load your prototype cells from a NIB (or storyboard), they will have the same height as defined in the UITableView.

Solution from picciano is not working in my app.

I used this delegate function instead, and it works :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
        CGFloat *theCellHeight=xxx;
        //determine here which section/row it is, and the desired height for it, put it in "theCellHeight"
        return theCellHeight;
}

Hope it helped you,

Remark : Nevertheless, it seems there is no way to get the cell's content/reference because cellAtIndexPath freezes the application when called inside that function. So you have to build your table in an Array programatically, then create it, so it makes the NIB less useful.

Even though you are correctly setting the height of your row in heightForRowAtIndexPath, your cell's contents will always have a fixed height because your are loading your cell from a nib. To make the cell content change height dynamically you will need to instead create your cell and its subviews programmatically in cellForRowAtIndexPath, using the same height calculation you are using in heightForRowAtIndexPath:

My guess is that you aren't handling the resizing of the cell properly.

I'm not sure when the cell's frame is set, but I'm guessing it's set AFTER you return a cell from your tableView:cellForRowAtIndexPath: method, that means that the cell is created or reused, and THEN it's frame is set.

What you should do is override the cell's setFrame: method and, after calling super, modifying it's subviews frames to fit the new size.

You could also use autoresizing masks on it's subviews on Interface Builder so they adapt automatically to the new frame.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top