我有 UITable 视图,在合适的单元格中,我使用自己的标签和非常长的文本。结果这个 uilabel 花费了大约 100 mb。(在这种情况下 - 内存警告然后崩溃)。我该如何解决?提前致谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *message;
CGSize textSize;
CGSize fullSize = CGSizeMake(300, 100000);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
message = [[UILabel alloc] init];
message.frame = CGRectMake(10, 30, 100, 20);
message.font = [UIFont systemFontOfSize:12.0f];
message.textAlignment = UITextAlignmentLeft;
message.textColor = [UIColor whiteColor];
message.tag = 103;
message.numberOfLines = 100000;
message.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:message];
[message release];
}
else
{
message = (UILabel *)[cell.contentView viewWithTag:103];
}
NSData *messageData = [[grandData objectAtIndex:indexPath.row] objectForKey:@"content"];
NSString *tempContent = [[NSString alloc] initWithData:messageData encoding:NSUTF8StringEncoding];
message.text = tempContent; // text about 10000 symbols in length
[tempContent release];
textSize = [message.text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:fullSize lineBreakMode:UILineBreakModeWordWrap];
message.frame = CGRectMake(10, 30, textSize.width, textSize.height + 6);
return cell;
}
我使用消息标签(不是 cell.textLabel),因为有一些标签显示其他信息(我削减了多余的代码)。单元格的高度会根据文本的数量动态变化
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath