TableCell中的UILabel永远不会更改字体或颜色

时间:2014-06-05 02:21:46

标签: ios objective-c fonts uilabel uitableview

我已经试图找到一个解决方案已经有一段时间了,但没有运气。我有一个TableViewController有一些单元格,所有单元格都有UILabel个文字。我尝试使用以下代码更改字体和颜色:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:12.0f];
    cell.textLabel.textColor = [UIColor redColor];

    return cell;
}

此代码应该是正确的,将字体和颜色设置为UILabel,但它永远不会改变。也许我需要从Interface Builder改变一个设置?也许设置我需要禁用的属性?

奇怪的是,即使从Interface Builder开始,字体和颜色也永远不会改变。

1 个答案:

答案 0 :(得分:2)

请尝试以下代码: -

@interface TestTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation TestTableViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.tableView.delegate=self;
  self.tableView.dataSource=self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * reuseIdentifier = nil ;
    UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

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

    cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:12.0f];
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.text = [NSString stringWithFormat:@"Number: %d",indexPath.row];
  return cell;
}
相关问题