ios - 自定义 UITableView 标题部分

标签 ios objective-c swift 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;
}

关于ios - 自定义 UITableView 标题部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15611374/

相关文章:

带有 xcode 8 GM 的 iOS 10 GM 由于 roundedCorners 和 clipsToBounds 导致 View 消失

ios - __LP64__ 和 __arm64__ 在 Cocoa 中存在哪些定义可以在编译时区分平台?它们在哪里或如何定义?

objective-c - 成为没有动画的第一响应者

objective-c - 在 UITextField 中禁用 "Define"

ios - Koyami 日历未正确显示 future 日期

ios - iPhone : touches on UIViewController

iphone - iOS : Create one sideded dropshadow

ios - 在 Swift 中使用 sysctlbyname()

ios - 滚动页面时如何隐藏和显示 View

ios - 如何在 swift 上编写 DatagramPacket 代码?