UITableViewCell字体不变

Phi*_*unt 16 iphone uitableview

我试图在UITableViewCell填充tableview时使用以下代码自定义a的字体.

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

    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }

    // Set up the cell  
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];   
    NSString *temp = [[NSString alloc] initWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
    cell.textLabel.text = temp;
    [[cell textLabel] setTextColor:[UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1]];
    [[cell textLabel] setFont:[UIFont systemFontOfSize:12.0]];  

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

对于我的生活,我不知道为什么它不会改变字体!如果我在单元格文本中进行硬编码,上面的代码也能正常工作cell.textLabel.text = @"TEST";

有什么建议?谢谢!

Jas*_*oco 29

首先,你应该自动释放你的细胞.你现在正在疯狂地泄露记忆.

其次,你应该更新字体tableView:willDisplayCellForRow:atIndexPath:.如果您使用的是标准表视图,它将对您的单元格进行更改(随机时间),您需要tableView:willDisplayCellForRow:atIndexPath:在数据源方法中而不是在数据源方法中执行字体更改,背景颜色等操作.

另请参见此主题:什么是 - [UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?


Man*_*nni 8

@Jason Coco:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}
Run Code Online (Sandbox Code Playgroud)