gpt4 book ai didi

objective-c - UITableViewCell中的文本居中对齐问题

转载 作者:IT老高 更新时间:2023-10-28 11:27:44 26 4
gpt4 key购买 nike

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

一些代码:

- (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;
}

上面的文字没有居中。

我也试过 willDisplayCell 方法:

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

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

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

这些都不会对文本对齐产生任何影响。我的想法已经用完了,任何帮助将不胜感激。

提前干杯。

最佳答案

不知道它是否对您的特定问题有帮助,但是如果您使用 initWithStyle:UITableViewCellStyleDefault

UITextAlignmentCenter 确实有效

关于objective-c - UITableViewCell中的文本居中对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3467288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com