ios - TableCell 中的 UILabel 永远不会改变字体或颜色

标签 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 中,字体和颜色也永远不会改变。

最佳答案

试试下面的代码:-

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

关于ios - TableCell 中的 UILabel 永远不会改变字体或颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24050602/

相关文章:

ios - UIScrollView 不滚动 iOS

ios - 如何在 Objective-C 的不同 View 中共享本地变量

ios - 在 Swift 3 迁移后从编译器警告中找到不兼容的 Objective-C 类别定义

emacs - 在 Emacs 中混合字体

wpf - XAML - 使用来自另一个程序集的资源字典中的字体

iOS 应用程序在 Apple 批准过程中崩溃但对我来说不是

ios - 在 Xcode 中构建时添加变量

ios - SwiftUI ForEach View 第一次未更新

iOS 错误 : Heap corruption detected, free list is damaged and Incorrect guard value: 0

html - Mac OSX 上的 Chrome 在基本拉丁 block 中呈现字体