代码之家  ›  专栏  ›  技术社区  ›  user2588945

如何将标题与UITableView一起滚动?

  •  47
  • user2588945  · 技术社区  · 11 年前

    我有一个 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;
    }
    
    6 回复  |  直到 6 年前
        1
  •  73
  •   Community CDub    8 年前

    只有当 UITableViewStyle 表的属性设置为 UITableViewStylePlain 。如果您将其设置为 UITableViewStyleGrouped ,标题将随单元格一起向上滚动。

    这个答案取自 this question .

    这个解决方案可以在不考虑标头数量的情况下工作。

        2
  •  44
  •   Ilario    7 年前

    以新方法创建sectionHeader视图,然后添加到末尾:

    self.tableView.tableHeaderView = sectionHeader;
    
        3
  •  21
  •   Ketan Sodvadiya    6 年前

    如果有动态页眉视图或单一视图,请选择表格样式 select table view style as <code>group</code> table

        4
  •  12
  •   Anjali Prasad    6 年前

    更改 UITableViewStyle 从…起 Plain Grouped 使节标题与其他单元格一起滚动。

        5
  •  6
  •   Rendel    8 年前

    如果你写 tableView.tableHeaderView = sectionHeader 您将丢失实际的表视图标头。
    如果您想要将表视图标题与节标题一起使用,则必须设置 UITableViewStyle 属性到 UITableViewStyleGrouped .
    此外,如果您想自动计算截面标头高度,您可以 return UITableViewAutomaticDimension 在里面 heightForHeaderInSection 方法如下:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return UITableViewAutomaticDimension;
    }
    
        6
  •  4
  •   Hindu    11 年前

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

    tableView.tableHeaderView = Header;
    

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

    谢谢