ios - 自定义 UITableView 标题部分

我想为每个部分自定义 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;
}

https://stackoverflow.com/questions/15611374/

相关文章:

objective-c - Swift References 中的 _ 下划线代表什么?

objective-c - 在 Objective-C 中大写或更改 NSString 的大小写

objective-c - 在objective-c中@符号代表什么?

objective-c - Objective-C 中可为空、__nullable 和 _Nulla

ios - 使用 AVFoundation AVPlayer 循环播放视频?

ios - 以编程方式调用电话

objective-c - NSInvocation 傻瓜?

iphone - 如何在 Objective-C 2.0 中将方法标记为已弃用?

ios - 较小时 UIScrollView 的中心内容

objective-c - Objective-C 中是否存在强类型集合?