[英]UILabel inside custom UITableViewCell not drawing at the correct size
[英]Increasing size of Custom UITableViewCell that contains 2 UILabel
我正在UITableView
。 在我的应用程序中,我将在2个部分中显示一些数据,并且有一个custom cell
,实际上在customCell
我有2个UILabel
label1 & label2
。 第一section
是可以的,因为它具有简单的数据来显示,但是当我显示第二部分时,我要显示4行,则3行中的string
要大得多。 因此,为了显示标签,我正在增加Cell和UILabel视图的高度。
为了增加单元格的高度,我实现了以下方法heightForRowAtIndexPath
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (1 == indexPath.section && 0 != indexPath.row)
{
return _profileTableView.rowHeight + 50.0;
}
else
{
return _profileTableView.rowHeight;
}
}
heightForRowAtIndexPath
方法的问题在于,仅第三行和第四行的大小会增加,当我尝试将新框架设置为label1和label2时,没有任何变化出现。标签的显示在单元格中非常小。
这就是我实现cellForRowAtIndexPath
方法的方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ISClientProfileCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = (CustomCell *)[nib objectAtIndex:0];
cell.label1.font = [UIFont fontWithName:defaultFont size:18];
cell.label2.font = [UIFont fontWithName:defaultBoldFont size:18];
if(0 == indexPath.section)
{
// first section is fine
}
else
{
switch (indexPath.row)
{
case 0:
{
cell.label1.text = @"Data 1:";
[cell.label1 sizeToFit];
CGRect frame = cell.label1.frame;
frame.origin.y = 11;
cell.label1.frame = frame;
frame = cell.label2.frame;
frame.origin.x = cell.label1.frame.size.width + cell.label1.frame.origin.x+5;
frame.origin.y = 11;
cell.label2.frame = frame;
cell.label2.text = [[NSUserDefaults standardUserDefaults] valueForKey:key];
[cell.label2 sizeToFit];
break;
}
case 1:
{
cell.label2.numberOfLines = 5;
cell.label2.lineBreakMode = NSLineBreakByClipping;
cell.label1.text = @"Data 2:";
[cell.label1 sizeToFit];
CGRect frame = cell.label1.frame;
frame.origin.y = 11;
cell.label1.frame = frame;
frame = cell.label2.frame;
frame.origin.x = cell.label1.frame.size.width + cell.label1.frame.origin.x+5;
frame.origin.y = 11;
cell.label2.frame = frame;
cell.label2.text = [[NSUserDefaults standardUserDefaults] valueForKey:aKey];
[cell.label2 sizeToFit];
break;
}
case 2:
{
cell.label2.numberOfLines = 5;
cell.label2.lineBreakMode = NSLineBreakByClipping;
cell.label1.text = @"Data 2:";
[cell.label1 sizeToFit];
CGRect frame = cell.label1.frame;
frame.origin.y = 11;
cell.label1.frame = frame;
frame = cell.label2.frame;
frame.origin.x = cell.label1.frame.size.width + cell.label1.frame.origin.x+5;
frame.origin.y = 11;
cell.label2.frame = frame;
cell.label2.text = [[NSUserDefaults standardUserDefaults] valueForKey:bKey];
[cell.label2 sizeToFit];
break;
}
case 3:
{
cell.label2.numberOfLines = 5;
cell.label2.lineBreakMode = NSLineBreakByClipping;
cell.label1.text = @"Data 2:";
[cell.label1 sizeToFit];
CGRect frame = cell.label1.frame;
frame.origin.y = 11;
cell.label1.frame = frame;
frame = cell.label2.frame;
frame.origin.x = cell.label1.frame.size.width + cell.label1.frame.origin.x+5;
frame.origin.y = 11;
cell.label2.frame = frame;
cell.label2.text = [[NSUserDefaults standardUserDefaults] valueForKey:cKey];
[cell.label2 sizeToFit];
break;
}
}
}
return cell;
}
我进行了足够的搜索,但没有答案适合我。 请帮忙。 提前致谢。
试试这个代码:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section !=0 && indexPath.row !=0)
{
return 44.0 + 50.0;
}
else
{
return _profileTableView.rowHeight;
}
return 44.0;
}
我希望它将帮助您解决问题。
尝试为您的单元格使用其他CellIdentifier。 这样,tableview不会在reusablecell调用上放错单元格的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.