一段时间以来,我一直在努力寻找解决方案,但没有成功。我有一个包含许多单元格的 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/