我有一个UITable,我正在扩展以显示“更多信息”文本。我遇到的问题是文本可以是不同的大小,即indexpath.section1可能是1200高,其中索引path.section2是300高。
在重新使用单元格之前,我的代码工作正常,标签框架的大小不适合新标签。因此,尽管单元格的大小正确,但单元格内的标签仍然具有原始大小的框架,因此如果它太小,文本会被切断,我会得到大量的空白区域,如果它太大,那么除非我滚动到在田野的中间。
我做错了什么?请帮忙吗?
这是我的代码: -
static NSUInteger const k1 = 1;
// Declare references to the subviews which will display the data.
UILabel *lbl1 = nil;
static NSString *kCellID = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
if ((indexPath.section == 8) || (indexPath.section == 9) || (indexPath.section == 10) || (indexPath.section == 11) || (indexPath.section == 12) || (indexPath.section == 13) || (indexPath.section == 14)|| (indexPath.section == 15) || (indexPath.section == 16))
{
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 200)];
}
else if ((indexPath.section == 4) || (indexPath.section == 5))
{
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 1200)];
}
lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
lbl1.tag = k1;
lbl1.textColor = [UIColor blackColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.highlightedTextColor = [UIColor whiteColor];
lbl1.opaque = NO;
lbl1.adjustsFontSizeToFitWidth = TRUE;
lbl1.numberOfLines = 0;
lbl1.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:lbl1];
} else {
lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
}
cell.accessoryView.tag = indexPath.row;
lbl1.text = [NSString stringWithFormat:@"%@", cellValue];
return cell;
}
}
}
答案 0 :(得分:0)
您需要在标签上设置自动调整遮罩。这将导致在调整其superview时调整其大小。适当的设置是:
lbl1.autoresizingMask = UIViewAutoresizingFlexibleHeight;