【发布时间】:2012-11-24 04:11:59
【问题描述】:
我通过 tableView 的委托方法提供了一个自定义部分标题。它在平面模式下工作正常,但我无法确定分组样式中的正确边距。
有谁知道如何使节标题与 tableView 单元格对齐?
【问题讨论】:
标签: ios uitableview
我通过 tableView 的委托方法提供了一个自定义部分标题。它在平面模式下工作正常,但我无法确定分组样式中的正确边距。
有谁知道如何使节标题与 tableView 单元格对齐?
【问题讨论】:
标签: ios uitableview
您可以创建自定义标签并相应地调整其框架。 例如。
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc] initWithFrame:aTableView.tableHeaderView.frame];
UILabel *label=[[UILabel alloc] initWithFrame: CGRectMake(//set frame of the label as you want)];
[label setFont:[UIFont fontWithName: size:]];
[label setTextColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0.0f, 1.0f)];
[label setShadowColor:[UIColor redColor];
label.backgroundColor=[UIColor clearColor];
if(section==0)
{
[label setText://set your section0 title];
}
else if(section ==1)
{
[label setText://set your section1 title];
}
[view addSubview:label];
[label release];
return [view autorelease];
}
Hope this helps :)
【讨论】:
没有对每种口味都进行过测试,但这是一个很好的起点。
在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
将 tableView.style 参数传递给您自己提供标头的方法。
创建一个类似框架的视图CGRect(0,0,CGRectGetWidth(tableView.frame), CGRectGetHeight(tableView.frame)
但是添加一个带有框架的子视图CGRectInset(frame, 30,0)
如果你有一个分组的 tableView 样式。将 autoresizingMask 设置为灵活宽度即可。
稍微修改了一下,我不得不为 SectionHeaderView 的创建方法提供一个附加参数,因为presentationStyle FullScreen和Formsheet的边距不同。
CGFloat margin = 0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
margin = style == UITableViewStyleGrouped ? modalStyle == UIModalPresentationFormSheet ? 28 : 38 : 0;
}
else {
margin = style == UITableViewStyleGrouped ? 5 : 0;
}
UIView *view = [[UIView alloc] initWithFrame:CGRectInset(frame, margin, 0)];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubview:view];
【讨论】: