[英]Determine custom section header frame when tableView mode grouped
我通過tableView的委托方法提供了一個自定義節標題。 它在平面模式下可以正常工作,但是我無法找出分組樣式中的正確邊距。
有誰知道如何使節標題與tableView單元格對齊?
您可以創建一個自定義標簽並相應地調整其框架。 例如。
- (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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.