我有一个 UITableView,它有时会在加载时停止响应。我不知道为什么会发生这种情况,我想知道这是否可能是我在 cellForRowAtIndexPath 中所做的单元格布局,因为我的内容是动态的,因此每次都必须采用子视图的布局。
有没有更好的方法来实现我想要的?或者这还有什么问题吗?
这是我写的代码:
首先创建自定义单元格并将不同视图添加到内容视图的方法:
- (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 = clearColor;
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;
}
这里是调整每个单元格子视图大小的方法:
// 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;
}
最后是 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);
}
}
非常感谢您的任何建议!