如何滚动头与UITableView的相处?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何滚动头与UITableView的相处?相关的知识,希望对你有一定的参考价值。
我有一个头一个UITableView
。我现在有问题的是,头不滚动表。我需要它滚动出屏幕(上图),当用户滚动表查看最多。所述的tableview滚动但头在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;
在一种新的方法创建sectionHeader视图,然后添加到末尾:
self.tableView.tableHeaderView = sectionHeader;
当表的UITableViewStyle
属性设置为UITableViewStylePlain
这种行为仅仅是常见的。如果你已经将它设置为UITableViewStyleGrouped
,头会随着细胞向上滚动。
这个答案是从this question拍摄。
该解决方案的工作不管头的数量。
从UITableViewStyle
改变Plain
到Grouped
使节头与其他细胞中滚动。
如果你在表中的单个头,那么你可以使用tableHeaderView
如下:
tableView.tableHeaderView = Header;
或者,如果你有多个头表比你需要使用的不是纯表组表。
谢谢
如果你写tableView.tableHeaderView = sectionHeader
你将失去实际表视图标题。
如果你想有表视图标头节头一起,你必须设置UITableViewStyle
属性UITableViewStyleGrouped
。
此外,如果你想计算节头高度自动,你可以在这样return UITableViewAutomaticDimension
方法heightForHeaderInSection
:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return UITableViewAutomaticDimension;
以上是关于如何滚动头与UITableView的相处?的主要内容,如果未能解决你的问题,请参考以下文章
UITableViewCell 按钮在滚动之前不显示选中或取消选中状态
iOS:如何将 UITableViewCell 附件按钮向左移动
在两个 UITableView 之间转换时如何模仿 UINavigation 动画行为