5

I am setting the height of cell using heightForRowAtIndexPath: method returning 100. But in cellForRowAtIndexPath method, the cell's frame height is always 44px.

Following is my code.

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    [cell setBackgroundColor:[UIColor redColor]];
    [cell.contentView setBackgroundColor:[UIColor redColor]];
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    NSLog(@"%f %f", cell.frame.size.height, cell.contentView.frame.size.height);
    return  cell;
}

The NSLog statement prints "44px 44px".

I am able to get the correct height using this statement

CGFloat height = [tableView heightForRowAtIndexPath:indexPath];
Ajumal
  • 1,048
  • 11
  • 33
Dhawal
  • 185
  • 2
  • 11

1 Answers1

5

Your Delegate Method will not be called while allocating the cell. initWithStyle allways returns cells with height of 44 unless you overwrite it in a subclass.

Jonathan Cichon
  • 4,396
  • 16
  • 19
  • Ok. Thanks. will have to overwrite it in subclass then. – Dhawal Jan 24 '13 at 15:13
  • 1
    @Dhawal actualy you dont really need to, the height will be set correct befor the cell is Displayed (if you have set the delegate). – Jonathan Cichon Jan 24 '13 at 15:18
  • i can confirm that setting the height is required in the initWithStyle method even with the correct delegate methods. – jacob bullock Oct 29 '14 at 02:38
  • what do u mean initWithStyle? I am giving height for row UITableViewAutomaticDimension and I see the frame is larger than 44 but in init method the size is 44 – Yestay Muratov Oct 19 '17 at 09:35