UITableViewCell中UILabel宽度的动态计算 [英] dynamic calculation of UILabel width in UITableViewCell

查看:126
本文介绍了UITableViewCell中UILabel宽度的动态计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于UITableViewCellStyle1(等等)构建一个自定义的应用程序内设置视图。



我正在尝试动态计算单元格的左标签(标题)的宽度,以确定右边文本字段的宽度可以是多少。



当我在模拟器中启动应用程序并加载视图时,标题标签的宽度为零,直到我向下滚动视图,并且单元格不可见,然后当我滚动回尺寸按预期调整。我相信这可能需要做细胞再利用的事情。



我需要单元格中的标题标签,以便在视图加载时具有正确的宽度,而不是在一次看不见之后。

$ b (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath
{
static NSString * reuseIdentifier = @SettingsCell;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if(!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease];
[cell setBackgroundColor:[UIColor baeDarkGrayColor]];


UILabel * cellLabel = [cell textLabel];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor clearColor]];

[cell setUserInteractionEnabled:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}

//使用相应的数据填充单元格。
NSDictionary * tableSection = [_tableData objectAtIndex:indexPath.section];

//设置标签
UILabel * textLabel = [cell textLabel];
NSString * labelString = [[tableSection objectForKey:@itemTitles] objectAtIndex:indexPath.row];
[textLabel setText:labelString];

// CGSize constrainedSize;
// constrainedSize.width = MAXFLOAT;
// constrainedSize.height = MAXFLOAT;

CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font / * constrainedToSize:constrainedSize * /];

NSLog(@text label width:%f,textLabelSize.width);


//设置附件视图
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@itemAccessories] objectAtIndex:indexPath.row] integerValue];
switch(accessoryType)
{
case BAESettingsTableCellAccessoryDisclosureIndicator:
{
UIImageView * disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@AccessoryDisclosure.png ]];
[cell setAccessoryView:disclosureView];
[disclosureView release];
break;
}
case BAESettingsTableCellAccessoryOnOffSwitch:
{
UISwitch * onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[onOffSwitch setOn:YES];
[cell setAccessoryView:onOffSwitch];
[onOffSwitch release];
break;
}
case BAESettingsTableCellAccessoryNone:
//默认值:
// break;
{

CGRect cellFrame = [cell frame];

float detailTextFieldWidth = cellFrame.size.width - (textLabelSize.width + 50);
float detailTextFieldHeight = cellFrame.size.height - 1;

NSLog(@详细文本字段计算的框架宽度,高度,%f,%f,detailTextFieldWidth,detailTextFieldHeight);


CGRect框架= CGRectMake(cellFrame.origin.x,cellFrame.origin.y,detailTextFieldWidth,detailTextFieldHeight);


UITextField * textField = [[UITextField alloc] initWithFrame:frame];
NSString * detailLabelString = [[tableSection objectForKey:@itemDetailStrings] objectAtIndex:indexPath.row];
textField.text = detailLabelString;
textField.textColor = cell.detailTextLabel.textColor;
textField.textAlignment = UITextAlignmentRight;
textField.borderStyle = UITextBorderStyleRoundedRect;
[cell setAccessoryView:textField];
[textField release];
break;
}

}

返回单元格;
}


解决方案

明确设置字体可以解决您的问题。

  CGSize textLabelSize = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:17]]; 

Gory Detals:

当第一个由于字体大小为0,此代码段返回零宽度。

  [textLabel.text sizeWithFont:textLabel.font] 

我通过显示单元格字体的点大小来计算出来。

  NSLog(@font size:%f,cell.textLabel.font.pointSize); 

点数大小为零,直到单元格关闭屏幕并返回,正如您所描述的那样。 / p>

I am building a custom, in-application settings view based on UITableViewCellStyle1 (or so).

I am trying to dynamically calculate the width of the left label (title) of a cell to determine how wide my text field on the right can be.

When I launch the app in the simulator and the view loads, the width of the title labels is zero until I scroll down the view and a cell is not visible, then when I scroll back up the size adjusts as expected. I believe this might have to do something with cell reusage.

I need the title labels in the cells to have their correct width when the view loads, not after they have gone out of sight one time.

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
  {
      static NSString* reuseIdentifier = @"SettingsCell";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if ( !cell )
{   
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease];
    [cell setBackgroundColor:[UIColor baeDarkGrayColor]];


    UILabel* cellLabel = [cell textLabel];
    [cellLabel setTextColor:[UIColor whiteColor]];
    [cellLabel setBackgroundColor:[UIColor clearColor]];

    [cell setUserInteractionEnabled:YES];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}

// Populate cell with corresponding data.
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section];

// Set the label
UILabel* textLabel = [cell textLabel];
NSString* labelString = [[tableSection objectForKey:@"itemTitles"] objectAtIndex:indexPath.row];
[textLabel setText:labelString];

   //   CGSize constrainedSize;
  //    constrainedSize.width = MAXFLOAT;
 // constrainedSize.height = MAXFLOAT;

CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font /*constrainedToSize:constrainedSize*/];

NSLog(@"text label width: %f", textLabelSize.width);    


// Set the accessory view
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@"itemAccessories"] objectAtIndex:indexPath.row] integerValue];
switch ( accessoryType )
{
    case BAESettingsTableCellAccessoryDisclosureIndicator:
    {
        UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]];
        [cell setAccessoryView:disclosureView];
        [disclosureView release];
        break;
    }
    case BAESettingsTableCellAccessoryOnOffSwitch:
    {
        UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        [onOffSwitch setOn:YES];
        [cell setAccessoryView:onOffSwitch];
        [onOffSwitch release];
        break;
    }
    case BAESettingsTableCellAccessoryNone:
        // default:
        // break;
    {

        CGRect cellFrame = [cell frame];

        float detailTextFieldWidth = cellFrame.size.width - ( textLabelSize.width + 50 );
        float detailTextFieldHeight = cellFrame.size.height - 1;

        NSLog(@"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight);


        CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight);


        UITextField* textField = [[UITextField alloc] initWithFrame:frame];
        NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row];
        textField.text = detailLabelString;
        textField.textColor = cell.detailTextLabel.textColor;
        textField.textAlignment = UITextAlignmentRight;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        [cell setAccessoryView:textField];
        [textField release];
        break;
    }

}

return cell;
   }

解决方案

Explicitly setting the font solves your problem.

CGSize textLabelSize = [textLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];

Gory Detals:
When the tableview first loads, this snippet returns zero width due to the font size being 0.

[textLabel.text sizeWithFont:textLabel.font]

I figured this out by displaying the point size of the cell font.

NSLog(@"font size: %f", cell.textLabel.font.pointSize);

The point size was zero until the cell went off screen and came back, just as you described.

这篇关于UITableViewCell中UILabel宽度的动态计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆