Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cell in heightForRowAtIndexPath?

I have created custom cells in my app.I want to get the each cell in HeightForRowAtIndexPath.Please tell me how can i get the custom cell in this method.I have tried this code but this causes infinite loop & finally crash the app.

HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];

EDIT:

I Have tried this but it gives me cell height as zero.

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

    static NSString *cellIdentifier = @"HomeCell";
    HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    float tv_view_height=cell.tv_post.frame.size.height;
    float like_count_height=cell.label_like_count.frame.size.height;
    float first_comment_height=cell.first_comment.frame.size.height;
    float second_comment_height=cell.second_cmment.frame.size.height;
    float third_comment_height=cell.third_comment.frame.size.height;

    Post *user_post=[arr_post objectAtIndex:indexPath.row];
    float comment_count=[user_post.comment_count intValue];
    if(comment_count<=0)
    {
        first_comment_height=0;
        second_comment_height=0;
        third_comment_height=0;


    }
    else if(comment_count==1)
    {
        second_comment_height=0;
        third_comment_height=0;

    }
    else if(comment_count==2)
    {

        third_comment_height=0;
    }
     float like_count=[user_post.like_count intValue];
    if(like_count<=0)
    {

        like_count_height=0;
    }
    float total_height=tv_view_height+like_count_height+first_comment_height+second_comment_height+third_comment_height;
    NSLog(@"total heigh is %f'",total_height);
    return total_height;
}

Please tell which is the best way?

like image 223
TechChain Avatar asked Dec 05 '22 20:12

TechChain


2 Answers

How to get cell in heightForRowAtIndexPath?

It's impossible, because when -heightForRowAtIndexPath is called, no cells are created yet. You need to understand how the UITableView works:

  1. UITableView asks it's datasource how many sections it will have

    -numberOfSectionsInTableView

    At this point there are no cells created.

  2. UITableView asks it's datasource how many rows each section will have

    -numberOfRowsInSection

    At this point there are no cells created.

  3. UITableView asks it's delegate height of each visible row, to know where cells will be located

    -heightForRowAtIndexPath

    At this point there are no cells created.

  4. UITableView asks it's datasource to give it a cell to display at given index path

    -cellForRowAtIndexPath

    At this point the cell is created.

The height of each cell you can calculate from data model. You don't need the cell – you already know the frame width that will contain a comment, you know it's content, you know it's font, you know linebreak mode, etc. So, you can calculate height. For example:

CGFloat commentsHeight = 0;
Post *user_post = [arr_post objectAtIndex:indexPath.row];

for (NSString *comment in user_post.comments)
{
    CGRect commentrect = [comment boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 18, FLT_MAX)
                                          options:(NSStringDrawingUsesLineFragmentOrigin)
                                        attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}
                                          context:nil];
    commentsHeight += commentrect.size.height;
}

And you can calculate height of the other components of cell from its data model.

But now, in 2015, it's not the best way. You really would be better to read the tutorials, which showed @Zil, and do it with Autolayout.

like image 183
ifau Avatar answered Dec 20 '22 22:12

ifau


You should declare an array for storing TableView cells in cellForRowAtIndexPath and you can use stored cells in heightForRowAtIndexPath. Lets Try using this.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *cellIdentifier = @"HomeCellID";

     HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

     if (!cell) {
          cell = [[[HomeCell alloc] init] autorelease];
     }

     // Store table view cells in an array
     if (![tableViewCells containsObject:cell]) {
          [tableViewCells addObject:cell];
     }

     return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if([tableViewCellsArray objectAtIndex:indexPath.row]) {
          HomeCell *cell = (HomeCell *)[tableViewCells objectAtIndex:indexPath.row];
          // Process your Code
     }

     return yourCalculatedCellHeight;
}
like image 31
Md. Reaz Murshed Khan Avatar answered Dec 21 '22 00:12

Md. Reaz Murshed Khan