0

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

- (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}}
4

1 回答 1

0

UITableViewCell 的 textLabel 字段是常规的 UILabel。您可以设置此属性以使其缩小文本以适应:

 theCell.textLabel.adjustsFontSizeToFitWidth = YES;

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

 theCell.textLabel.minimumFontSize  = whatever

看看 UILabel 上的文档,它会对你有很大帮助。

于 2012-07-04T00:01:57.180 回答