2013-02-19 41 views
2

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

下面的代码:

-(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。这似乎是一个不必要的步骤,所以我想确保我没有做错什么。

感谢

编辑 我得到相同的效果我是否设置字体的cellForRowAtIndexPathwillDisplayCell

+0

,这是对我工作的罚款。什么是问题。详细告诉我。 – Nirav 2013-02-19 20:28:36

+0

我必须手动调用viewDidAppear中的reloadData才能显示字体。我认为这会导致数据在视图控制器加载时加载两次,每当视图控制器出现时加载一次。对我来说似乎没有必要。 – Dcritelli 2013-02-19 20:32:05

+1

我仍然建议你在你的单元格初始化中更改你的cell.textLabel.font,否则将设置单元格在屏幕上显示的所有内容。 – Ares 2013-02-19 21:42:14

回答

6

尝试做这样的

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

就是这样。初始化时点的大小为零。我想这是导致字体加载失败,它恢复到默认状态。 – Dcritelli 2013-02-19 20:54:24

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 
} 
+0

忘了提及这是我尝试的第一种方式。 – Dcritelli 2013-02-19 20:28:12

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; 
}