2

我没有做任何复杂的事情。只是试图在表格单元格上设置字体。如果我在视图控制器出现后重新加载表格数据,或者表格滚动并且单元格自行更新,则字体显示正常。但是如果我只是在第一次加载时设置了 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];
}

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

谢谢

编辑 无论我设置字体cellForRowAtIndexPath还是willDisplayCell

4

3 回答 3

6

尝试这样做

    cell.textLabel.font = [UIFont fontWithName:@"MuseoSlab-500" size:17.0];
于 2013-02-19T20:50:24.360 回答
1

将 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
}
于 2013-02-19T20:25:36.303 回答
0

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

- (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;
}
于 2013-02-19T20:49:44.923 回答