中心对齐UITableViewCell的页脚

alv*_*pez 7 objective-c uitableview ios

我怎样才能将UITableViewCell的页脚对齐?

我尝试使用以下代码,但它不起作用:

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    UILabel *footerLabel = [[UILabel alloc] init];
    footerLabel.text = @"Centered text";
    footerLabel.textAlignment = NSTextAlignmentCenter;
    return footerLabel.text;
}
Run Code Online (Sandbox Code Playgroud)

我也试过创建一个UIView,但我得到了一个 Incompatible pointer types returning 'UIView *' from a function with result type 'NSString *'

小智 24

此代码将以页脚文本为中心:

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
    footer.textLabel.textAlignment = NSTextAlignmentCenter;
}
Run Code Online (Sandbox Code Playgroud)

它将匹配现有的UITableView页脚样式,并且还可以正确处理设备旋转和不同宽度,而无需混淆帧或约束.


Ria*_*man 5

斯威夫特3:

override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
    let footer: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    footer.textLabel?.textAlignment = .center
}
Run Code Online (Sandbox Code Playgroud)

像魅力一样工作。:)


Dar*_*yte -1

只需使用tableView:viewForFooterInSection- 它需要一个视图(可以是您的标签)。