问题
I want to implement a tableview which shows an expandable cell for a specific row, so I create my custom table cell which will be expanded if setting its expandContent as following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *shouldExpand = [self.datasource objectAtIndex:indexPath.row];
if([shouldExpand isEqualToString:@"expand"]){
[cell setExpandContent:@"Expand"];
}
else{
[cell setTitle:@"a line"];
}
return cell;
}
however, in order to tell tableview the row height, I need to implement the following code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return [cell cellHeight];
}
The problem is heightForRowAtIndexPath method will call 1000 times of tableView:cellForRowAtIndexPath: if my datasource contains 1000 data, and it cost too much time.
how to fix the problem?
回答1:
no u should find the size of cell first then send the hight calculated, dont call tableView:cellForRowAtIndexPath: it will results in recursion instead, first calculate and send the height . for example
//say suppose you are placing the string inside tableview cell then u need to calculate cell for example
NSString *string = @"hello world happy coding";
CGSize maxSize = CGSizeMake(280, MAXFLOAT);//set max height
CGSize cellSize = [self.str sizeWithFont:[UIFont systemFontOfSize:17]
constrainedToSize:maxSize
lineBreakMode:NSLineBreakByWordWrapping];//this will return correct height for text
return cellSize.height +10; //finally u return your height
回答2:
how to fix the problem?
If you really have 1000 rows, you should consider not using dynamic row heights because even if you come up with a fast way to determine the row height the table will still need to ask about the height of each row separately. (In fact, if you really have 1000 rows, you should reconsider your whole design -- that's just too much data to look through with a linear interface.)
If you must use dynamic row heights for a large number of rows, you'll need to at least find a quick way to determine the height without creating the whole cell. Perhaps you can determine the factors that affect row height and come up with a very simplified method for calculating the height. If you can't do that, the it might make sense to calculate the height for each row once and then save the result with the row data so that you don't have to calculate it again until the data changes.
回答3:
This is the code i used to set the UITableViewCell height dynamically:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary* dict = [branchesArray objectAtIndex:indexPath.row];
NSString* address = [NSString stringWithFormat:@"%@,%@\n%@\n%@\n%@",[dict objectForKey:@"locality"],[dict objectForKey:@"city"],[dict objectForKey:@"address"],[dict objectForKey:@"contactNumber"], [dict objectForKey:@"contactEmail"]];
CGSize constraint = CGSizeMake(220, MAXFLOAT);
CGSize size = [address sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height1 = MAX(size.height, 110.0f);
return height1+20;
}
as well as set the frame in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath also
来源:https://stackoverflow.com/questions/17961951/how-to-specify-height-of-row