我正在研究一个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
是,只有第 3 行和第 4 行的大小增加,当我尝试将新框架设置为 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;
}
我进行了足够的搜索,但没有任何答案对我有用。请帮忙。提前致谢。