中心在UITableViewCell问题中对齐文本 [英] Center Align text in UITableViewCell problem

查看:87
本文介绍了中心在UITableViewCell问题中对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Objective-C和iPhone开发的新手,我在尝试将文本放在表格单元格中时遇到了问题。我搜索了谷歌,但解决方案是针对一个旧的SDK错误已修复,这些对我不起作用。

I'm kinda new to Objective-C and iPhone development and I've come across a problem when trying to center the text in a table cell. I've searched google but the solutions are for an old SDK bug that has been fixed and these don't work for me.

一些代码:

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

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = @"Please center me";
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    return cell;
}

上述内容并非文本中心。

The above doesn't center the text.

我也试过了willDisplayCell方法:

I have also tried the willDisplayCell method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.textLabel.textAlignment = UITextAlignmentCenter;
}

我尝试了一些旧发布的解决方案:

and I've tried some of the old posted solutions:

UILabel* label = [[[cell contentView] subviews] objectAtIndex:0];
label.textAlignment = UITextAlignmentCenter;
return cell;

这些都不会对文本对齐产生任何影响。我已经没想到任何帮助将是最受欢迎的。

None of these have any effect on the text alignment. I have run out of idea's any help would be most appreciated.

提前干杯。

推荐答案

不知道它是否有助于解决您的具体问题,但 UITextAlignmentCenter 如果您使用 initWithStyle:UITableViewCellStyleDefault

Don't know if it helps your specific problem, however UITextAlignmentCenter does work if you use initWithStyle:UITableViewCellStyleDefault

这篇关于中心在UITableViewCell问题中对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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