UITableViewCell with dynamic content, dynamic layout and thus a dynamic height
I have a UITableView which sometimes just stops responding when its been loaded. I have no clue why this happens and I was wondering whether it could be the layouting of the cells which I do in cellForRowAtIndexPath, because my content is dynamic and therefore the layout of the subviews has to be adopted each time.
Is there a better approach to achieve what I want? Or is there anything else wrong with this?
Here is the code I wrote:
First the method which creates the custom cell and adds the different views to the content view:
- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *titleLbl, *amountLbl, *title2Lbl, *numberLbl, *mergeLbl, *typeLbl;
UIColor *bgColor, *blackColor, *amountColor , *title2Color;
UIColor *clearColor = [UIColor clearColor];
bgColor = [UIColor clearColor];
title2Color = [UIColor colorWithRed:0.1 green:0.1 blue:0.6 alpha:1];
amountColor = [UIColor redColor];
blackColor = [UIColor blackColor];
//The number
numberLbl = [[UILabel alloc] initWithFrame:CGRectZero];
numberLbl.tag = kNumberLblTag;
numberLbl.font = [UIFont systemFontOfSize:12];
numberLbl.textAlignment = UITextAlignmentLeft;
numberLbl.textColor = blackColor;
numberLbl.backgroundColor = clearColor;
numberLbl.numberOfLines = 0;
[cell.contentView addSubview:numberLbl];
[numberLbl release];
amountLbl = [[UILabel alloc] initWithFrame:CGRectZero];
amountLbl.tag = kamountLblTag;
amountLbl.font = [UIFont systemFontOfSize:12];
amountLbl.textAlignment = UITextAlignmentLeft;
amountLbl.textColor = amountColor;
amountLbl.backgroundColor = clearColor;
amountLbl.numberOfLines = 0;
[cell.contentView addSubview:amountLbl];
[amountLbl release];
mergeLbl = [[UILabel alloc] initWithFrame:CGRectZero] ;
mergeLbl.tag = kMergeLblTag;
mergeLbl.font = [UIFont systemFontOfSize:12];
mergeLbl.textAlignment = UITextAlignmentLeft;
mergeLbl.textColor = blackColor;
mergeLbl.backgroundColor = clearColor;
mergeLbl.numberOfLines = 0;
[cell.contentView addSubview:mergeLbl];
[mergeLbl release];
typeLbl = [[UILabel alloc] initWithFrame:CGRectZero];
typeLbl.tag = kTypeLblTag;
typeLbl.font = [UIFont systemFontOfSize:12];
typeLbl.textAlignment = UITextAlignmentLeft;
typeLbl.textColor = blackColor;
typeLbl.backgroundColor = clearColor;
typeLbl.numberOfLines = 1;
[cell.contentView addSubview:typeLbl];
[typeLbl release];
titleLbl = [[UILabel alloc] initWithFrame:CGRectZero];
titleLbl.tag = kTitleLblTag;
titleLbl.font = [UIFont systemFontOfSize:14];
titleLbl.textAlignment = UITextAlignmentLeft;
titleLbl.textColor = blackColor;
titleLbl.backgroundColor = clearCol开发者_StackOverflow社区or;
titleLbl.numberOfLines = 0;
[cell.contentView addSubview:titleLbl];
[titleLbl release];
title2Lbl = [[UILabel alloc] initWithFrame:CGRectZero];
title2Lbl.tag = ktitle2LblTag;
title2Lbl.font = [UIFont systemFontOfSize:12];
title2Lbl.textAlignment = UITextAlignmentLeft;
title2Lbl.textColor = title2Color;
title2Lbl.backgroundColor = clearColor;
title2Lbl.numberOfLines = 0;
[cell.contentView addSubview:title2Lbl];
[title2Lbl release];
UIView *bgView = [[UIView alloc]initWithFrame:CGRectZero];
bgView.backgroundColor = bgColor;
cell.backgroundView = bgView;
[bgView release];
NSLog(@"tableViewCellWithReuseIdentifier");
return cell;
}
And here the method which resizes the subviews for each cell:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSUInteger r = row+(section*100);
if (cell == nil) {
cell = [self tableViewCellWithReuseIdentifier: CellIdentifier];
}
UIColor *bgColor, *clearColor = [UIColor clearColor];
//set the rePointer to the actual array
if (r < [results count]) {
rePointer = results;
//NSLog(@"results r: %i", r);
}else {
r = [indexPath row];
rePointer = rejects;
//NSLog(@"rejects r: %i", r);
}
NSString *noTxt = [[NSString alloc]initWithFormat:@"%i.", r+1];
CGSize boundingSize = CGSizeMake(35, 100);
UIFont *sysFont = [UIFont systemFontOfSize:12];
CGSize requiredSize = [noTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat requiredHeight = requiredSize.height;
UILabel *numberLbl = (UILabel*)[cell viewWithTag:kNumberLblTag];
numberLbl.frame = CGRectMake(5, 5, 35, requiredHeight);
numberLbl.text = noTxt;
NSMutableString *amountTxt = [[NSMutableString alloc]initWithString:@"amount: "];
[amountTxt appendString:[NSString stringWithFormat:@"%@", [[rePointer objectAtIndex:r] objectForKey:@"amount"]]];
boundingSize = CGSizeMake(35, 100);
sysFont = [UIFont systemFontOfSize:12];
requiredSize = [amountTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
requiredHeight = requiredSize.height;
UILabel *amountLbl = (UILabel*)[cell viewWithTag:kamountLblTag];
amountLbl.frame = CGRectMake(5, 20, 35, requiredHeight);
amountLbl.text = amountTxt;
if ([[[rePointer objectAtIndex:r] objectForKey:@"count"] intValue] > 0) {
NSString *mergedTxt = [[NSString alloc]initWithString:@"Count"];
boundingSize = CGSizeMake(35, 100);
sysFont = [UIFont systemFontOfSize:12];
requiredSize = [mergedTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
requiredHeight = requiredSize.height;
UILabel *mergeLbl = (UILabel*)[cell viewWithTag:kMergeLblTag];
mergeLbl.frame = CGRectMake(5, 50, 35, requiredHeight);
mergeLbl.text = mergedTxt;
[mergedTxt release];
}else {
UILabel *mergeLbl = (UILabel*)[cell viewWithTag:kMergeLblTag];
mergeLbl.frame = CGRectZero;
mergeLbl.text = @"";
}
NSString *type;
if (![[[rePointer objectAtIndex:r] objectForKey:@"type"] isEqualToString:@"NotSet"]) {
if ([[[rePointer objectAtIndex:r] objectForKey:@"type"] isEqualToString:@"[XYZ]"]) {
type = @"X";
}else{
int typeLength = [[[rePointer objectAtIndex:r] objectForKey:@"type"] length];
type = [[[rePointer objectAtIndex:r] objectForKey:@"type"] substringWithRange:NSMakeRange(1, typeLength-2)];
}
UILabel *typeLbl = (UILabel*)[cell viewWithTag:kTypeLblTag];
typeLbl.frame = CGRectMake(5, 65, 35, 20);
typeLbl.text = type;
}else {
UILabel *typeLbl = (UILabel*)[cell viewWithTag:kTypeLblTag];
typeLbl.frame = CGRectZero;
typeLbl.text = @"";
}
NSString *titleTxt = [[NSString alloc]initWithString:[[[rePointer objectAtIndex:r] objectForKey:@"h_title"]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];
boundingSize = CGSizeMake(190, 1000);
sysFont = [UIFont systemFontOfSize:14];
requiredSize = [titleTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
requiredHeight = requiredSize.height;
CGFloat titleHeight = requiredHeight;
UILabel *titleLbl = (UILabel*)[cell viewWithTag:kTitleLblTag];
titleLbl.frame = CGRectMake(50, 5, 190, requiredHeight);
titleLbl.text = titleTxt;
[titleTxt release];
NSString *title2Txt = [[NSString alloc]initWithString:[[[rePointer objectAtIndex:r] objectForKey:@"title2"]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];
sysFont = [UIFont systemFontOfSize:12];
requiredSize = [title2Txt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
requiredHeight = requiredSize.height;
UILabel *title2Lbl = (UILabel*)[cell viewWithTag:ktitle2LblTag];
title2Lbl.frame = CGRectMake(50, titleHeight + 10, 190, requiredHeight);
title2Lbl.text = title2Txt;
[title2Txt release];
/*
CGFloat height = [[[rePointer objectAtIndex:r] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
height += [[[rePointer objectAtIndex:r] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
height += 20;
*/
cell.contentView.backgroundColor = clearColor;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(@"cellForRowAtIndexPath");
/*
cell.backgroundView.frame = CGRectMake(0, 0, 320, height-1);
cell.backgroundView.backgroundColor = bgColor;
*/
return cell;
}
And finally the heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
row += section*100;
NSUInteger rejectsIndex = [indexPath row];
if (row < [results count]) {
CGFloat height = [[[results objectAtIndex:row] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
height += [[[results objectAtIndex:row] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
height += 20;
return MAX(90, height);
}else {
CGFloat height = [[[rejects objectAtIndex:rejectsIndex] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
height += [[[rejects objectAtIndex:rejectsIndex] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
height += 20;
return MAX (90, height);
}
}
Thanks a lot for any advice!
I think your code is overly complicated and thus inefficient. Maybe you should consider using a custom table view cell which lets you access all the elements clean and directly.
I also suggest to cache the size measurements which are potentially slow.
Because you use tag to identify the subview in the cell. Be care of one thing, DON'T use 0 for tag number, or some tag number as the same as the cell's tag value.
The method viewWithTag: will return the first subView with the tag number, if the cell's tag is the same as the one you desired, it will return cell, not a subview.
Eiko's suggestion is right, you should consider using a custom table view cell.
Turned out that it was an iOS bug.
精彩评论