Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios TableCell中的UILabel从不更改字体或颜色_Ios_Objective C_Fonts_Uilabel_Uitableview - Fatal编程技术网

Ios TableCell中的UILabel从不更改字体或颜色

Ios TableCell中的UILabel从不更改字体或颜色,ios,objective-c,fonts,uilabel,uitableview,Ios,Objective C,Fonts,Uilabel,Uitableview,我一直在试图找到解决这个问题的办法,但很长一段时间都没有成功。我有一个TableViewController和许多单元格,所有单元格都有一个带有一些文本的UILabel。我正在尝试使用以下代码更改字体和颜色: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [menuItem

我一直在试图找到解决这个问题的办法,但很长一段时间都没有成功。我有一个
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 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;
}
@接口TestTableViewController()
@属性(弱、非原子)IBUITableView*tableView;
@结束
@TestTableViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.tableView.delegate=self;
self.tableView.dataSource=self;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回5;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*reuseIdentifier=nil;
UITableViewCell*单元格=(UITableViewCell*)[tableView出列可重用CellWithIdentifier:reuseIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
cell.textlab.font=[UIFont fontWithName:@“Times New Roman”大小:12.0f];
cell.textLabel.textColor=[UIColor redColor];
cell.textLabel.text=[NSString stringWithFormat:@“编号:%d”,indexPath.row];
返回单元;
}

谢谢你的建议,我只是尝试了一下,但还是没什么。我刚刚编辑了答案。确保标题上有,并将视图控制器设置为viewDidLoad中的代理和数据源。非常感谢,只有在选择“无任何事情发生”时,它才起作用:/他们都有一个序列来显示另一个TableRowController。你是什么意思?我以为你只想为单元格设置文本颜色和字体?如果您还需要其他信息,可以更新您的问题吗?谢谢,但已成功解决了问题。文本是从界面生成器中手动添加的,因为@Bamswold评论通过代码分配文本解决了问题。您在哪里分配此UILabel的文本?我怀疑UITableViewCell的textLabel属性未引用要更改的同一UILabel。上面的代码适用于我,但是我首先将text属性设置为test-cell.textLabel.text=@“Testing changes”;谢谢你的建议,我不知道界面生成器中的文本不足以改变字体。