部分左侧的UITableView标题(如Spotlight)

一直在寻找这一点,我还没有find办法做到这一点。 我想重新将iPhone的Spotlight的部分标题复制到UITableView中。

众所周知,滚动时,“常规”表格视图标题会保留在部分的顶部。 但是有一种标题是我在Springboard的Spotlight页面以外从未见过的:在那里,标题跨越了该部分的整个高度,而不是卡在该部分的顶部,而是在左侧。

这到底是怎么回事?

好问题。 我做了一个小实验。 它几乎看起来像聚光灯下的景象。 但缺乏一个导入function。 如果碰撞,较低的“图像”不会将上面的图像推到顶部。

我怀疑没有使用私有框架的内置解决scheme。


我做到了这一点: 在这里输入图像说明

正如你可以看到在顶部的两个标题图像重叠。 他们不像普通的标题对彼此推。 而我不得不停用细胞分离器。 如果我使用他们,他们会出现在整个细胞。 所以你必须自己画。 没什么大不了的。

但我不知道如何修复重叠。

这是发生这种情况的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)]; contentView.backgroundColor = [UIColor lightGrayColor]; UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 34, 34)] autorelease]; imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", section]];; [contentView addSubview:imageView]; return [contentView autorelease]; } 

好吧,我已经做了类似于音乐应用程序和聚光灯search的东西。

我没有子类UITableView,我刚刚通过它的scrollViewDidScroll方法跟踪部分,并添加了标题视图左侧的tableView(所以你将不得不放在你的viewController的视图右侧的tableview,这意味着你可以'使用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; } 

我已经构build了代码,以便任何不需要的标题视图被删除,所以我们需要一个方法,在需要时返回视图:

 -(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

在横向方向 希望这可以帮助 :)。

好消息:请参阅如何实现高级表格视图标题的答案

结果http://i.minus.com/jyea3I5qbUdoQ.png