【问题标题】:UITableViewCell custom font only displays after table updateUITableViewCell 自定义字体仅在表格更新后显示
【发布时间】:2013-02-20 04:50:24
【问题描述】:

我没有做任何复杂的事情。只是试图在表格单元格上设置字体。如果我在视图控制器出现后重新加载表格数据,或者表格滚动并且单元格自行更新,则字体显示正常。但是如果我只是在第一次加载时设置了 tableview 属性,它就不能正确显示。

代码如下:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"listItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [pickerList objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    cell.backgroundColor = [UIColor AMFDarkBlueColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.textColor = [UIColor AMFRedColor];
    cell.textLabel.font = [UIFont fontWithName:@"MuseoSlab-500" size:cell.textLabel.font.pointSize];
}

就像我说的,如果我在viewDidAppear 中调用reloadData,它会起作用。这似乎是一个不必要的步骤,所以我想确保我没有做错什么。

谢谢

编辑 无论我将字体设置为cellForRowAtIndexPath 还是willDisplayCell,我都会得到相同的效果

【问题讨论】:

  • 这对我来说很好用。有什么问题。详细告诉我。
  • 我必须在 viewDidAppear 中手动调用 reloadData 才能显示字体。我在想这会导致数据在视图控制器加载时加载两次,每次视图控制器出现时加载一次。对我来说似乎没有必要。
  • 我仍然建议您更改单元格初始化中的 cell.textLabel.font 否则将设置单元格将在屏幕上显示的所有字体。

标签: ios objective-c uitableview


【解决方案1】:

这样试试

    cell.textLabel.font = [UIFont fontWithName:@"MuseoSlab-500" size:17.0];

【讨论】:

  • 就是这样。初始化时点大小为零。我猜这是导致字体加载失败并恢复为默认值。
【解决方案2】:

在单元格启动后将 cell.textLabel.font = ... 更改为正确。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"listItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        cell.textLabel.font = [UIFont fontWithName:@"MuseoSlab-500" size:cell.textLabel.font.pointSize];
    }
//Continue cell config
}

【讨论】:

    【解决方案3】:

    这对我来说很好,试一试:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    
    UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 30.0 ];
    cell.textLabel.font = myFont;   
    [cell.textLabel setText:@"Text"];
    
    return cell;
    }
    

    【讨论】:

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