我想将标题集中在tableview上,但是我遇到了2个问题。首先我没有正确的高度,其次UILabel看起来不像默认标题 - 字体/字体大小/颜色等...是否有更好的方法来居中它,和/或有没有办法制作它看起来像是默认标题。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
//section text as a label
UILabel *lbl = [[UILabel alloc] init];
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = @"Header";
[lbl setBackgroundColor:[UIColor clearColor]];
return lbl;
}
答案 0 :(得分:26)
这应该可以解决您的问题。在xcode 6 / ios8中测试
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextAlignment:NSTextAlignmentCenter];
return [sectionTitles objectAtIndex:section];
}
答案 1 :(得分:25)
您还必须实现tableView:heightForHeaderInSection来调整标题高度:
在UITableViewDelegate协议参考文档中,您会找到:
的tableView:viewForHeaderInSection:
讨论
例如,返回的对象可以 是UILabel或UIImageView对象。 表格视图会自动调整 节标题的高度为 容纳返回的视图对象。 此方法仅在以下情况下正常工作 tableView:heightForHeaderInSection:是 也实施了。
要使标题标注居中,您必须在将其对齐设置为居中之前指定标签框。 要获取标准字体,请使用SystemFontOfSize:
还要注意你正在创建内存泄漏你应该返回一个自动释放的视图
尝试这样的事情:
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont systemFontOfSize:12];
希望这有帮助, 文森特
答案 2 :(得分:19)
如果您定位 iOS6及更高版本,如果您只想将标题标题居中,则不必提供自己的标题视图。
只需实施
- tableView:willDisplayHeaderView:forSection:
并设置标签的textAligment属性:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter;
}
}
答案 3 :(得分:15)
如果您定位iOS6及更高版本,如果您只想将标题标题居中,则不必提供自己的标题视图。
实施
- tableView:willDisplayHeaderView:forSection:
并设置标签的textAligment属性:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textAlignment = .Center
}
}
答案 4 :(得分:5)
这是我的版本,您需要截取普通标题背景的屏幕截图,并将其1px宽的片段保存为“my_head_bg.png”并将其添加到项目中。这样,它看起来完全正常:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
lbl.text = @"My Centered Header";
lbl.textColor = [UIColor whiteColor];
lbl.shadowColor = [UIColor grayColor];
lbl.shadowOffset = CGSizeMake(0,1);
lbl.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"my_head_bg"]];
lbl.alpha = 0.9;
return lbl;
}
答案 5 :(得分:5)
要调用此方法,首先应该实现
titleForHeaderInSection
然后将调用tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
方法
Swift解决方案:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textAlignment = Localize().isRTL ? .Right : .Left
headerView.textLabel?.text = partsDataSource[section]
headerView.textLabel?.textColor = UIColor ( red: 0.0902, green: 0.2745, blue: 0.2745, alpha: 1.0 )
headerView.textLabel?.font = UIFont(name: UIDecorator.sharedInstance.PRIMARY_FONT, size: 14.0)
headerView.contentView.backgroundColor = UIDecorator.sharedInstance.currentTheme.lightShadeColor
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return " "
}
答案 6 :(得分:1)
试试这个
UILabel *tableHeader = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[tableHeader setText:@"This is header"];
[tableHeader setTextAlignment:UITextAlignmentCenter];
UITableView *newTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 280, 200) style:UITableViewStylePlain];
[newTable setBackgroundColor:[UIColor clearColor]];
[newTable setTableHeaderView:tableHeader];
self.view = newTable;
答案 7 :(得分:0)
在Xamarin:
// Center Header
public override void WillDisplayHeaderView(UITableView tableView, UIView headerView, nint section)
{
if (headerView.GetType() == typeof(UITableViewHeaderFooterView))
{
UITableViewHeaderFooterView tableViewHeaderFooterView = (UITableViewHeaderFooterView)headerView;
tableViewHeaderFooterView.TextLabel.TextAlignment = UITextAlignment.Center;
}
}
答案 8 :(得分:-4)
要更改高度,请实施此方法:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
return 15;
}
我不确定默认标头的东西 - 这应该比它更容易。也许有人有一个漂亮的背景图像,您可以使用白色字体。但是对于居中的事情,试试这个:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,15)];
label.textAlignment = UITextAlignmentCenter
label.text = @"Text Should Be Centered";