UITableViewCell height with 0 rows
I've got a moderately uncustomized UITableView in my application that presents an array of up to 6 peers for a networking game. When no peers are found the numberOfRowsInSection is 0, so when it loads up, there's no rows in the table:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 0;
}
Trouble is, the height of each cell is not adjusted to match heightForRowAtIndexPath unless the numberOfRowsInSection is > 0. I hard-coded in "1" for numberOfRowsInSection and the height is adjusted as expected.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return kTableRowHeight;
}
Can anyone tell me why this is? Once the data that is being presented is long开发者_运维知识库er than "0" length, will the height of the table cells jump to the correct height? I'd like them to be the correct height even if the row length is 0.
Try to set the rowHeight
property on the UITableView
. My guess is that it doesn't even call heightForRowAtIndexPath
unless there's actually a cell in that row.
(In general, by the way, if all your rows are the same height, you should use rowHeight
instead of heightForRowAtIndexPath
- see the docs.)
That works. Just add [self.tableView setRowHeight:XX]
in viewDidLoad()
.
精彩评论