user4487951
user4487951

Reputation:

Access custom cell properties in heightForRowAtIndexPath

I have a custom UITableViewCell. I want to access the cells properties i.e. a UILabel etc. I tried inserting the following code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategorieCell *customCell = (CategorieCell *)[tableView cellForRowAtIndexPath:indexPath];

    return ...
}

When I run the app, it crashed without giving me error details. The problem is with the new customCell I'm creating. Is there another way I can access the customCell.m objects?

Upvotes: 2

Views: 1180

Answers (3)

tmpz
tmpz

Reputation: 368

Try to register your custom cell class like so:

[self.tableView registerClass:[CategorieCell class] forCellReuseIdentifier:NSStringFromClass([CategorieCell class]);

and then in -tableView:heightForRowAtIndexPath: do something like:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    CategorieCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CategorieCell class)];

}

Upvotes: 1

dminones
dminones

Reputation: 2296

About the crash, please note that you are using cellForRowAtIndexPath: wich is a method from the UITableViewDatasource that you have to implement, this method calls heightForRowAtIndexPath by default, so it will become a recursive

I assume that you want your custom cell in this method in order to get the height from it. The best way to achieve this is write a class method on CategorieCell that gives you the height for a cell with certain data.

Other option is extract a method with the code to get the uitableviewcell for example

(CategorieCell*) categorieCellForIndex:(NSIndex)index selected:(BOOL)selected{
...
}

Upvotes: 2

Matteo Gobbi
Matteo Gobbi

Reputation: 17707

In heightForRowAtIndexPath should never be called cellForRowAtIndexPath.

The first one is called before the second one, and if you need to access to a label (for example to calculate the height of the text) you can normally init a cell.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static CategorieCell *cell;
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        cell.frame = CGRectMake(0, 0, tableView.frame.size.width-tableView.contentInset.left-tableView.contentInset.right, cell.frame.size.height);
        [cell layoutIfNeeded];
    }

    cell.label.text = myDatasourceText;

    CGFloat cellHeight = ....

    return cellHeight;
}

NOTE 1: I used dequeueReusableCellWithIdentifier supposing you are using Interface Builder, otherwise you need to use alloc] initWithStyle:...];

NOTE 2: As you see I'm setting the frame of the cell. This is needed because otherwise your cell will be (320 x 44) as default. You instead could be in iPhone 6/6+ (i.e. screen width: 414) or in an iPad, and you could need to calculate an height of a label according with his width and his text, and for this reason you need to set the frame of the cell.

NOTE 3: I'm assuming you have a set of equal cell structure, and for this reason I'm using a static cell, so it will be reused without allocate other useless cells.

Upvotes: 1

Related Questions