【问题标题】:How to make UITableView section headers like in CoreDataBooks?如何制作像 CoreDataBooks 中的 UITableView 部分标题?
【发布时间】:2013-10-05 10:15:14
【问题描述】:

我一直想知道如何在 Apple 的示例应用程序 CoreDataBooks 中实现 UITableView 部分标题。请观看此视频:

video on youtube

  1. 这样的标头看起来与普通标头不同。我以前从未见过这样的标题。
  2. 当您滚动表格时,标题不会向上移动,直到它被下一个标题从下方推动。

我浏览了 CoreDataBooks 代码的每一行,但我找不到标题的外观和行为方式。我唯一能找到的是,如果您从方法 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 返回空字符串,那么(而不是我预期的空标题)根本没有标题。有什么建议?我该怎么做?

【问题讨论】:

  • 这是 UITableViewStylePlain 中的默认行为。
  • 如果您使用 UITableViewStyleGroup - 您的标题将移动到顶部“下方”。如果你使用 UITableViewStylePlain - 像视频这样的行为是默认的。并且标题是默认的。

标签: ios objective-c uitableview


【解决方案1】:

我不确定是否需要,但似乎他们使用自定义剖面视图。你可以通过实现 UITableViewDelegate "viewForHeaderInSection" 来获得这个结果

使用示例:

-(UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Header image have the same heigth that section 
    UIImageView *letterUIImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:<header image>]];

    UILabel *letterUILabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, 300, 18)] autorelease];

    [letterUIImageView setAlpha:0.5];

    [letterUILabel setTextColor:[UIColor whiteColor]];
    [letterUILabel setBackgroundColor:[UIColor clearColor]];
    [letterUILabel setFont:[UIFont fontWithName: @"HelveticaNeue-Bold" size: 14.0f]]; 
    [letterUIImageView addSubview:letterUILabel];

    [letterUILabel setText:[self titleForHeaderInSection:section]];

    return  letterUIImageView;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多