Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change tableview row height in ios 5

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

like image 864
Maxime Avatar asked Oct 14 '11 00:10

Maxime


2 Answers

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.

like image 61
MANIAK_dobrii Avatar answered Sep 24 '22 14:09

MANIAK_dobrii


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.

like image 25
tomidelucca Avatar answered Sep 21 '22 14:09

tomidelucca