我想为每个部分自定义 UITableView
标题。到目前为止,我已经实现了
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
这个 UITabelViewDelegate
方法。我想要做的是获取每个部分的当前标题,然后将 UILabel
添加为 subview 。
到目前为止,我无法做到这一点。因为,我找不到任何东西来获取默认的节标题。第一个问题,有什么办法可以得到默认的section header?
如果不可能,我需要创建一个容器 View ,它是一个 UIView
但是,这次我需要设置默认背景颜色、阴影颜色等。因为,如果你仔细查看部分 header ,已经自定义了。
如何获取每个节标题的这些默认值?
最佳答案
你可以试试这个:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
NSString *string =[list objectAtIndex:section];
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
关于ios - 自定义 UITableView 标题部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611374/