UITableViewCell的textLabel的字体更改未视觉更改 [英] UITableViewCell's textLabel's Font Change Not Visually Changed

查看:211
本文介绍了UITableViewCell的textLabel的字体更改未视觉更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将UIViewController推送到我的UINavigation控制器上时:

When I push a UIViewController onto my UINavigation controller like:

[(UINavigationController *)self.parentViewController pushViewController:[[[Fonts alloc] initWithNibName:@"Fonts" bundle:nil] autorelease] animated:YES];

其中Fonts.xib是一个UIView,只有UITableView由一个Fonts对象控制,该对象是UIViewController的子类并充当UITableView的dataSource和delegate。

Where Fonts.xib is a UIView with only UITableView controlled by a Fonts object that is a subclass of UIViewController and acts as the UITableView's dataSource and delegate.

在Fonts对象中,我创建了一个UITableViewCell,如:

In the Fonts object I create a UITableViewCell like:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"BlahTableViewCell"];

    if (!cell) {
        cell = [[UITableViewCell alloc]
                initWithStyle: UITableViewCellStyleDefault
                reuseIdentifier: @"BlahTableViewCell"];
        [cell autorelease];  // Delete for ARC
    }

    return cell;

}

然后我在这里更改单元格的字体:

And then I change the font of the cell here:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell.textLabel setFont:[(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontWithSize:cell.textLabel.font.pointSize]];
    cell.textLabel.text = [(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontName];
}

listOfFonts是UIFont对象的NSArray。

listOfFonts is an NSArray of UIFont objects.

当视图出现时,它看起来像 UITableView没有更改字体

When the view appears it looks like UITableView without changed fonts

如果我在UITableView上调用reloadData,或者如果我用手指将UITableViewCells拖出屏幕并让它们反弹,则会重新绘制它们并且单元格显示的视图带有标签他们的字体改变了。

If I call reloadData on the UITableView or if I drag the UITableViewCells off screen with my finger and let them bounce back they are redrawn and the view the cells display with the labels having their fonts changed.

似乎问题是UITableViewCells被过早绘制。如果我延迟它们的绘制,一切看起来都是正确的,但是当UINavigationController将我的视图滑动到位时,我希望UITableView能够正确显示。

It seems like the issue is the UITableViewCells are being drawn too early. If I delay the drawing of them everything looks correct but I want the UITableView to be displaying correctly when the UINavigationController slides my view into place.

知道我做错了什么?

编辑:我向Dropbox上传了一个简单明了的问题示例。 http://dl.dropbox.com/u/5535847/UITableViewIssue.zip

I uploaded a simple and straightforward example of my issue to Dropbox. http://dl.dropbox.com/u/5535847/UITableViewIssue.zip

推荐答案

解决了它!

好的,所以我的确是与原始海报相同的问题,这就是问题。

Ok so I was having exactly the same issues as the original poster and this was the problem.

造成问题的一行是:

[cell.textLabel setFont:[(UIFont *)[self.listOfFonts objectAtIndex:indexPath.row] fontWithSize:cell.textLabel.font.pointSize]];

具体来说,您的问题是因为您尝试将单元格的textLabel提供给自己的pointSize,但是PointSize不存在,所以发生了如此奇怪的错误。对我来说,我注意到由于奇异矩阵是不可逆的,变换失败了。一旦我将标准值硬编码为我的pointSize,我就立即看到所有标签都用正确的字体绘制。注意:这对于重绘的原因是有意义的,因为那时你的textLabel确实有一个pointSize。

Specifically, your issue is because you're trying to feed the cell's textLabel its own pointSize, but pointSize doesn't exist yet so strange bugs occur instead. For me, I noticed that a "transform" was failing due to a singular matrix being non-invertible. As soon as I hardcoded a standard value as my pointSize I saw all my labels draw with the proper font instantly. Note: this makes sense as to why a redraw worked, because then your textLabel does indeed have a pointSize.

在任何情况下,你需要在这里明确设置你的pointSize,没有使用textLabel已经拥有的东西,因为在你重新加载一个单元格之前它没有任何东西。

In any case, you need to explicitly set your pointSize here, no using what the textLabel "already has" because it doesn't have anything until you're "reloading" a cell.

这篇关于UITableViewCell的textLabel的字体更改未视觉更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆