【问题标题】:UITableViewCell font size is not changingUITableViewCell 字体大小没有改变
【发布时间】:2011-06-07 22:07:58
【问题描述】:

我试图让我的 UITableViewCell 上的字体更小。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* CellIdentifier = @"Cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

    UILabel *nameLabel; 
    UISwitch *mySwitch;
    if ( cell == nil ) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault 
                                       reuseIdentifier: CellIdentifier] autorelease];

        nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake( 220, 13, 100, 20 )] autorelease];
        nameLabel.tag = 22;
        nameLabel.font = [UIFont boldSystemFontOfSize: 5.0];
        nameLabel.textColor = [UIColor blueColor];
        nameLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        nameLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: nameLabel];

        mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
        mySwitch.tag = ((400*(indexPath.section+1))+indexPath.row);
        [mySwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:mySwitch];
        cell.accessoryView = mySwitch;


    }
    else {
        nameLabel = (UILabel*)[cell.contentView viewWithTag: 22];
        mySwitch = (UISwitch *)[cell.contentView viewWithTag:((400*(indexPath.section+1))+indexPath.row)];
    }

    nameLabel.textLabel.text = @"Hello";


}

但标签的字体肯定不会是 5 号。它是正常字体和正常大小,可能大约是 12 号字体,或者任何默认值。为什么我不能更改字体大小?

【问题讨论】:

  • 是否有理由将 nameLabel 添加到单元格的 contentView 两次?
  • 打错了,对不起。我已经删除了其中一个。它仍然有同样的问题。

标签: iphone ios fonts uitableview uifont


【解决方案1】:

试试这个

而不是nameLabel.textLabel.text = @"Hello";

使用

nameLabel.text = @"Hello";

【讨论】:

    【解决方案2】:

    与您的问题无关,但无需将开关添加为子视图。设置附件视图应将其添加到单元格并保留它。

        [cell addSubview:mySwitch];
        cell.accessoryView = mySwitch;
    

    ...应该只是...

        [cell setAccessoryView:mySwitch];
    

    【讨论】:

      猜你喜欢
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多