【发布时间】:2011-03-28 21:26:17
【问题描述】:
我是 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;
这些都不会对文本对齐产生任何影响。我的想法已经用完了,任何帮助将不胜感激。
提前干杯。
【问题讨论】:
-
UITextAlignment 在 iOS 6.0 中已弃用,现在是 NSTextAlignment
-
cell.textLabel?.textAlignment = .center
标签: objective-c iphone ios4