how to set height dynamically in table from dictionary in iphone
In My TableView I am calculating my cell's height as per dynamic data,
Now here I wan开发者_如何学编程t to do one thing,
I want to calculate table's height for only one time ,
after that I want to store it in a array so that when that row come again it should not calculate the whole size.
for this I want to declare a dictionary. In that I want to access two things . Height in integer and status for height is calculated or not.
Now status will note status is true or not. If height is calculated then status is true and height will be stored.
now when I come to there again . it will check if status is true then height should be fetch from dictionary only .
how can I do this?
There is -heightForRowAtIndexPath
delegate method for that:
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dict = // get your dictionary for this indexPath
if([[dict valueForKey:@"status"] boolValue] == YES) {
return [[dict valueForKey:@"height"] floatValue];
} else {
return 44.0f;
}
}
I wrote this code assumming that your dictionary is like:
status -> NSNumber: bool [YES/NO]
height -> NSNumber: float
You can make this dictionary in -viewDidLoad
like:
NSNumber *status = [NSNumber numberWithBool:YES]; // or NO
NSNumber *height = [NSNumber numberWithFloat:60.0f]; // or 100.0f, for example
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:status,@"status",height,@"height"];
Then you can add this dictionary to some array and read it in -heightForRow...
精彩评论