UITableViewCell字体不变 [英] UITableViewCell Font Not Changing
问题描述
我试图在填充tableview时使用以下代码自定义 UITableViewCell
的字体。
I am trying to customize the font of a UITableViewCell
using the following code for when the tableview is populated.
- (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;
}
对于我的生活,我不知道为什么它不会改变字体!如果我在单元格文本中进行硬编码,例如 cell.textLabel.text = @TEST;
For the life of me I don't know why it won't change the font! Also the above code works fine if I hard code in what the cell text is such as cell.textLabel.text = @"TEST";
有什么建议吗?谢谢!
推荐答案
首先,你应该自动释放你的手机。你现在正在疯狂地泄露记忆。
First, you should autorelease your cell. You are leaking memory like crazy presently.
其次,你应该更新中的字体tableView:willDisplayCellForRow:atIndexPath:
。如果您使用标准表视图,它将对您的单元格进行更改(随机时间),您需要在 tableView中执行诸如字体更改,背景颜色等操作:willDisplayCellForRow:atIndexPath:
而不是数据源方法。
Second, you should update the font in tableView:willDisplayCellForRow:atIndexPath:
. If you are using a standard table view, it will make changes to your cells (at random times) and you will need to do things like font changes, background color, etc in the tableView:willDisplayCellForRow:atIndexPath:
instead of in the data source method.
另见此主题:什么是 - [UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?
这篇关于UITableViewCell字体不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!