10

Before ios 5 I would set the row height in a tableview like this:

self.tableView.rowHeight=71;

However, it doesn't work on iOS5.

Someone has an idea?

Thanks

4 Answers 4

18

Have you tried tableView:heightForRowAtIndexPath: from UITableViewDelegate?

You may set the row height to 71 by implementing tableView:heightForRowAtIndexPath: in your UITableView delegate (one who supports the UITableViewDelegate protocol).

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

First you should set a delegate of your tableView. Delegate should conform to the UITableViewDelegate protocol. Let's say we have a TableDelegate class. To conform to a UITableViewDelegate protocol one should have it in square brackets in it's declaration like this:

...    
@interface TableDelegate : UIViewController <UITableViewDelegate>
...
or
@interface TableDelegate : UIViewController <some_other_protocol, UITableViewDelegate>

Then you set the delegate:

...
// create one first
TableDelegate* tableDelegate = [[TableDelegate alloc] init];
...
self.tableView.delegate = tableDelegate;

In the end you should implement the tableView:heightForRowAtIndexPath: method in the TableDelegate implementation:

@implementation TableDelegate
...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 71.0;
}
...
@end

rowHeight

Just to clarify, using rowHeight should work just fine and perform better than constant returned from -tableView:heightForRowAtIndexPath: as Javier Soto points out in the comments. Also note, that if your UITableView has delegate returning height in -tableView:heightForRowAtIndexPath: and rowHeight property set, prior value is honoured.

1
  • 4
    Check Apple warning on using this implementation instead of setting rowHeight: There are performance implications to using tableView:heightForRowAtIndexPath: instead of the rowHeight property. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more). Dec 13, 2011 at 12:25
3

Im coding for iOS 5 and it actually works. You just need to implement the line you said in the:

 - (void)viewDidLoad

method after the:

[super viewDidLoad];

The:

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

method does not work if the TableView is empty. But if you use the rowHeight property it will work even if the view is empty.

1

This method is change the Height of row

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return  51.0f
};
0

Try setting the rowHeight before viewWillAppear:, e.g. right after creating your table view.

This made it work for me on iOS 5. On iOS 6 it's easier: you can set it anywhere.

The advantage of using rowHeight, as pointed out by others, is that you avoid the performance impact of tableView:heightForRowAtIndexPath:.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.