2013-02-19 38 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; 
}