在表格滚动的时候会出弹出一个当前组的名字
效果如下:
实现步骤:
1.创建一个label用于显示组名
CGPoint center = myTableView.center;
displayLabel = [[UILabel alloc] initWithFrame:CGRectMake(center.x-40, center.y-40, 80, 80)];
displayLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
displayLabel.textColor = [UIColor whiteColor];
displayLabel.font = [UIFont boldSystemFontOfSize:50];
[displayLabel setHidden:YES];
displayLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:displayLabel];
2.实现ScrollView的代理方法
// 开始滚动
- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
displayLabel.hidden = NO;
}
// 正在滚动
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
// label 的中心点相对表格的坐标
CGPoint point = [myTableView convertPoint:displayLabel.center fromView:self.view];
// 用于计算label再哪个组上
NSIndexPath *indexPath = [myTableView indexPathForRowAtPoint:point];
MyData *data = [dataArray objectAtIndex:indexPath.section];
displayLabel.text = data.name;
}
// 结束滚动
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
displayLabel.hidden = YES;
}