さて、私は音楽アプリとスポットライト検索が行うのと同様のことをしました。
UITableViewをサブクラス化しておらず、scrollViewDidScrollメソッドを使用してセクションを追跡し、tableViewの左側にヘッダービューを追加しました(したがって、viewControllerのビューの右側にtableviewを配置する必要があります。つまり、次のことができます。 t UITableViewControllerを使用します)。
このメソッドは、scrollViewDidScroll、ViewDidLoad、およびdidRotateFromInterfaceOrientationで呼び出す必要があります:(回転をサポートしている場合)
左側のスペースは、サポートするインターフェイスの向きのヘッダーのサイズと同じにする必要があることに注意してください。
-(void)updateHeadersLocation
{
for (int sectionNumber = 0; sectionNumber != [self numberOfSectionsInTableView:self.tableView]; sectionNumber++)
{
// get the rect of the section from the tableview, and convert it to it's superview's coordinates
CGRect rect = [self.tableView convertRect:[self.tableView rectForSection:sectionNumber] toView:[self.tableView superview]];
// get the intersection between the section's rect and the view's rect, this will help in knowing what portion of the section is showing
CGRect intersection = CGRectIntersection(rect, self.tableView.frame);
CGRect viewFrame = CGRectZero; // we will start off with zero
viewFrame.size = [self headerSize]; // let's set the size
viewFrame.origin.x = [self headerXOrigin];
/*
three cases:
1. the section's origin is still showing -> header view will follow the origin
2. the section's origin isn't showing but some part of the section still shows -> header view will stick to the top
3. the part of the section that's showing is not sufficient for the view's height -> will move the header view up
*/
if (rect.origin.y >= self.tableView.frame.origin.y)
{
// case 1
viewFrame.origin.y = rect.origin.y;
}
else
{
if (intersection.size.height >= viewFrame.size.height)
{
// case 2
viewFrame.origin.y = self.tableView.frame.origin.y;
}
else
{
// case 3
viewFrame.origin.y = self.tableView.frame.origin.y + intersection.size.height - viewFrame.size.height;
}
}
UIView* view = [self.headerViewsDictionary objectForKey:[NSString stringWithFormat:@"%i", sectionNumber]];
// check if the header view is needed
if (intersection.size.height == 0)
{
// not needed, remove it
if (view)
{
[view removeFromSuperview];
[self.headerViewsDictionary removeObjectForKey:[NSString stringWithFormat:@"%i", sectionNumber]];
view = nil;
}
}
else if(!view)
{
// needed, but not available, create it and add it as a subview
view = [self headerViewForSection:sectionNumber];
if (!self.headerViewsDictionary && view)
self.headerViewsDictionary = [NSMutableDictionary dictionary];
if (view)
{
[self.headerViewsDictionary setValue:view forKey:[NSString stringWithFormat:@"%i", sectionNumber]];
[self.view addSubview:view];
}
}
[view setFrame:viewFrame];
}
}
また、表示されているビューを保持するプロパティを宣言する必要があります。
@property (nonatomic, strong) NSMutableDictionary* headerViewsDictionary;
これらのメソッドは、ヘッダービューのサイズとX軸オフセットを返します。
-(CGSize)headerSize
{
return CGSizeMake(44.0f, 44.0f);
}
-(CGFloat)headerXOrigin
{
return 10.0f;
}
不要なヘッダービューが削除されるようにコードを作成したので、必要なときにいつでもビューを返すメソッドが必要です。
-(UIView*)headerViewForSection:(NSInteger)index
{
UIImageView* view = [[UIImageView alloc] init];
if (index % 2)
{
[view setImage:[UIImage imageNamed:@"call"]];
}
else
{
[view setImage:[UIImage imageNamed:@"mail"]];
}
return view;
}
これがどのように見えるかです:


lanscapeでどのように表示されるか、tableViewの左側に44pxを与えるために制約を使用しました
お役に立てれば :)。