【问题标题】:How to scroll the header along with the UITableView?如何与 UITableView 一起滚动标题?
【发布时间】:2013-10-28 12:39:48
【问题描述】:

我有一个带有标题的UITableView。我目前遇到的问题是标题不随表格滚动。当用户向上滚动表格视图时,我需要它滚出屏幕(上图)。表格视图滚动,但标题锁定在 UIView 的顶部。

谢谢

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
    sectionHeader.backgroundColor = [UIColor whiteColor];

    // add user profile image to _contentView
    UIImageView *userImageView;

    UIImage *userImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl]]];

    userImageView=[[UIImageView alloc]initWithImage:userImage];
    userImageView.frame=CGRectMake(10,10,90,100);

   [sectionHeader addSubview:userImageView];

   // return userImageView;

    // user name lable
    CGRect userNameFrame = CGRectMake(110, 60, 100, 50 );
    UILabel* userNameLabel = [[UILabel alloc] initWithFrame: userNameFrame];
    [userNameLabel setText: firstName];
    [userNameLabel setTextColor: [UIColor blackColor]];
    [userNameLabel setBackgroundColor:[UIColor clearColor]];
    [userNameLabel setFont:[UIFont fontWithName:@"DIN-Regular" size:14]];

    [sectionHeader addSubview:userNameLabel];

    // user last name label
    CGRect userLastNameFrame = CGRectMake(110, 75, 100, 50 );
    UILabel* userLastNameLabel = [[UILabel alloc] initWithFrame: userLastNameFrame];
    [userLastNameLabel setText: lastName];
    [userLastNameLabel setTextColor: [UIColor blackColor]];
    [userLastNameLabel setBackgroundColor:[UIColor clearColor]];
    [userLastNameLabel setFont:[UIFont fontWithName:@"DIN-Regular" size:14]];

    [sectionHeader addSubview:userLastNameLabel];

    // user checkin view
    UIView *userCheckinView = [[UIView alloc] initWithFrame:CGRectMake(10, 120, 280, 25)];
    userCheckinView.backgroundColor = customColorGrey;
    [sectionHeader addSubview:userCheckinView];

    // check in label
    UILabel* userCheckInLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
    [userCheckInLabel setText: @"CHECK-IN"];
    userCheckInLabel.backgroundColor = customColorGrey;
    userCheckInLabel.textColor = customColorIt;
    [userCheckInLabel setFont:[UIFont fontWithName:@"DIN-Regular" size:12]];

    [userCheckinView addSubview:userCheckInLabel];

    // image
    UIImageView *checkinImg = [[UIImageView alloc]
                               initWithImage:[UIImage imageNamed:@"classifica_geotag_C.png"]];
    checkinImg.frame = CGRectMake(5, 0, 24, 24);
    [userCheckinView addSubview:checkinImg];

    // check in label
    UILabel* userCheckInCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
    [userCheckInCountLabel setText: [checkinCount stringValue]];
    userCheckInCountLabel.backgroundColor = customColorGrey;
    userCheckInCountLabel.textColor = customColorIt;
    [userCheckInCountLabel setFont:[UIFont fontWithName:@"DIN-Regular" size:12]];

    [userCheckinView addSubview:userCheckInCountLabel];

    // user like view
    UIView *userLikeView = [[UIView alloc] initWithFrame:CGRectMake(10, 150, 280, 25)];
    userLikeView.backgroundColor = customColorGrey;
    [sectionHeader addSubview:userLikeView];

    // like label
    UILabel* userLikeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
    [userLikeLabel setText: @"LIKE"];
    userLikeLabel.backgroundColor = customColorGrey;
    userLikeLabel.textColor = customColorIt;
    [userLikeLabel setFont:[UIFont fontWithName:@"DIN-Regular" size:12]];

    [userLikeView addSubview:userLikeLabel];

    // image
    UIImageView *likeImg = [[UIImageView alloc]
                            initWithImage:[UIImage imageNamed:@"classifica_like_C.png"]];
    likeImg.frame = CGRectMake(5, 0, 24, 24);
    [userLikeView addSubview:likeImg];

    // user like label
    UILabel* userLikeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
    [userLikeCountLabel setText: [likesCount stringValue]];
    userLikeCountLabel.backgroundColor = customColorGrey;
    userLikeCountLabel.textColor = customColorIt;
    [userLikeCountLabel setFont:[UIFont fontWithName:@"DIN-Regular" size:12]];

    [userLikeView addSubview:userLikeCountLabel];


    // la mia bacheca like view
    userLaMiaView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 300, 25)];
    userLaMiaView.backgroundColor = [UIColor clearColor];
    [sectionHeader addSubview:userLaMiaView];


    // like label
    UILabel* userLaMiaLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 20)];
    [userLaMiaLabel setText:NSLocalizedString(@"LA MIA BACHECA", nil)];

    userLaMiaLabel.backgroundColor = [UIColor clearColor];
    userLaMiaLabel.textColor = customColorGrey;
    [userLaMiaLabel setFont:[UIFont fontWithName:@"DIN-Bold" size:10]];

    [userLaMiaView addSubview:userLaMiaLabel];

    // grey line view below la mia label
    userGreyLineView = [[UIView alloc] initWithFrame:CGRectMake(10, 248, 280, 1.5)];
    userGreyLineView.backgroundColor = [UIColor whiteColor];
    [sectionHeader addSubview:userGreyLineView];


    return sectionHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 210;
}

【问题讨论】:

  • UITableViewStyle 更改为 UITableViewStyleGrouped 对我有用

标签: ios objective-c uitableview


【解决方案1】:

这种行为仅在表的UITableViewStyle 属性设置为UITableViewStylePlain 时才常见。如果您将其设置为UITableViewStyleGrouped,标题将与单元格一起向上滚动。

此答案取自this question

无论标题数量如何,此解决方案都有效。

【讨论】:

  • self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; // Get headers to scroll
  • 这应该是公认的答案,因为它可以处理任意数量的部分,而不仅仅是一个。
【解决方案2】:

在新方法中创建sectionHeader视图,然后添加到最后:

self.tableView.tableHeaderView = sectionHeader;

【讨论】:

  • @user2588945 非常感谢,您可以标记我的答案吗? ;)
  • 这不是最好的解决方案。你应该喜欢@kokx的回答
  • @samir 与我看起来相同的答案有什么区别。谢谢
  • UITableViewStyle 更改为UITableViewStyleGrouped 对我有用
  • 我认为将 UITableViewStyle 改为 UITableViewStyleGrouped 是一种更好更简单的方法。
【解决方案3】:

如果您有动态标题视图或单视图选择表格样式

【讨论】:

    【解决方案4】:

    UITableViewStylePlain 更改为Grouped 使节标题与其他单元格一起滚动。

    【讨论】:

      【解决方案5】:

      如果您写tableView.tableHeaderView = sectionHeader,您将丢失实际的表格视图标题。
      如果您想拥有表格视图标题和部分标题,则必须将 UITableViewStyle 属性设置为 UITableViewStyleGrouped
      此外,如果您想自动计算节标题高度,您可以在heightForHeaderInSection 方法中使用return UITableViewAutomaticDimension,如下所示:

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          return UITableViewAutomaticDimension;
      }
      

      【讨论】:

        【解决方案6】:

        如果表中有一个标题,则可以使用tableHeaderView,如下所示:

        tableView.tableHeaderView = Header;
        

        或者,如果表中有多个标题,则需要使用组表而不是普通表。

        谢谢

        【讨论】:

        • 这对我很有用。我不得不在很多表格视图之上添加一个 UIImageView 。我对此进行了子类化,现在我可以添加 UIImageView 以及为每个部分添加标题。
        猜你喜欢
        • 2020-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多