2012-07-03 69 views
0

我正在尝试为我们的应用程序创建一个设置。我不确定这里发生了什么。我有一个UITableViewSyleGrouped,并在表的每个部分,有1行。对于我的特定行,它显示了该人的姓名。如果你点击它,然后它推到一个新的tableView,它有可供选择的人员列表,然后当你弹出时,标签被更新,但是当我从一个较小的名字转到一个更大的名字时,标签被截断。我正在尝试为我们的应用创建一个设置。一些字段如下所示:cell.textLabel没有调整大小

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 
    if (tableView == _settingsTableView) { 
     UITableViewCell *cell = nil; 

     NSNumber *aSection = [_tableArray objectAtIndex:indexPath.section]; 

     if ([aSection integerValue] == SOUNDS) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"]; 
      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SwitchCell"] autorelease]; 
      } 
      cell.textLabel.text = @"Sounds"; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; 
      cell.accessoryView = switchView; 
      [switchView setOn:[[Settings sharedInstance] playSounds] animated:NO]; // initialize value from Settings 
      [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
      [switchView release]; 
     } 
     else if ([aSection integerValue] == PERSON) { 
      cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"]; 
      if (cell == nil) { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"PersonCell"] autorelease]; 
      } 
      Person *p = [_personArray objectAtIndex:row]; 
      cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", p.firstName, p.lastName]; 
         NSLog(@"cL: %@", NSStringFromCGRect(cell.textLabel.frame)); 
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
    return cell; 
} 

我的PERSON部分为用户提供了更改人员的功能。在didSelectRowAtIndexPath方法的代码是

else { 
    Person *p = [_personArray objectAtIndex:row]; 
    NSUInteger oldRow = [_lastIndexPath row]; 
    if (oldRow != row) { 
     dmgr.currentPerson = p; 

     // Put checkmark on newly selected cell 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 

     // Remove checkmark on old cell 
     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 
     [_settingsTableView deselectRowAtIndexPath:_lastIndexPath animated:YES]; 
     self.LastIndexPath = indexPath; 

     // Update the cell 
     NSIndexPath *path = [NSIndexPath indexPathForRow:0 PERSON]; 
     UITableViewCell *theCell = [_settingsTableView path]; 

     theCell.textLabel.text = [NSString stringWithFormat:@"%@ %@", p.firstName, p.lastName]; 
     [theCell setNeedsDisplay];     
     NSLog(@"ceLL: %@", NSStringFromCGRect(theCell.textLabel.frame)); 

    } 
} 

会发生什么事,直到我点击标签上的标签被截断。 (例如John D ...而不是John Doe)。为什么标签不能更新?

我试着看着框架,我不确定这是否与它有关。我的输出是:

cL: {{0, 0}, {0, 0}} 
ceLL: {{10, 11}, {76, 21}} 

回答

0

UITableViewCell的textLabel字段是一个普通的UILabel。您可以设置该属性以使其缩小文本以适应:

theCell.textLabel.adjustsFontSizeToFitWidth = YES; 

您还可以设置最小字体大小

theCell.textLabel.minimumFontSize = whatever 

看看上的UILabel文档它会帮助你很多。

+1

minimumFontSize在iOS 6中已弃用 – whyoz