开发者

change tableview row height in ios 5

Before ios 5 I would set the row height in a tableview 开发者_StackOverflow社区like this:

self.tableView.rowHeight=71;

However, it doesn't work on iOS5.

Someone has an idea?

Thanks


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.


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.


This method is change the Height of row

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


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:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜