2011-03-03 46 views
6

已經搜索了很長一段時間,我仍然沒有找到辦法做到這一點。 我想重新將iPhone的Spotlight的部分標題複製到UITableView中。部分左側的UITableView標題(如聚光燈)

正如我們所知,滾動時,「常規」表格視圖標題在部分頂部保持可見。但是有一種標題是我在Springboard的Spotlight頁面以外從未見過的:在那裏,標題橫跨該部分的整個高度,並且不會卡在該部分的頂部,而是位於左側。

這到底是怎麼回事?

回答

5

好問題。我做了一個小實驗。它幾乎看起來像聚光燈下的風景。但它缺乏一個導入功能。如果較低的「圖像」碰撞,則不會將較高的圖像推到頂部。

我懷疑沒有使用私有框架的內置解決方案。


我實現了這個:enter image description here

正如你可以在上面看到重疊的兩個頭的圖像。他們不會像普通標題那樣互相推送。我必須停用細胞分離器。如果我使用它們,它們會出現在整個細胞中。所以你必須自己繪製它們。沒什麼大不了的。

但我不知道如何修復重疊。

這裏是讓這一切發生的代碼:

- (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]; 
} 
+0

爲了處理這個重疊的怪癖,也許我們可以嘗試'UIScrollViewDelegate'的'-scrollViewDidScroll:'?我會做我也能做的,但至少你的小原型看起來很有前途。謝謝! – Cyrille 2011-03-03 15:00:30

+0

@Matthias我設法得到想要的結果。看到我的回答如下 – Besi 2012-07-13 22:07:08

4

好吧,我已經做了類似的東西是什麼音樂應用程序和Spotlight搜索確實。我已經通過它的scrollViewDidScroll方法跟蹤了部分,並在tableView的左邊添加了標題視圖(所以你必須把tableview放在viewController的視圖的右邊,這是意味着你不能使用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; 
} 

下面是它的外觀:

the section's header is moving out of the screen as it's section does

the section's header is moving with the section's origin

它將如何看在lanscape,我用約束上給予44px中的的tableView

in landscape orientation 希望這有助於:)。

+1

甜美而簡單的執行。恭喜! – Cyrille 2012-09-26 16:20:15

+0

聰明的解決方案! – RPallas 2018-02-21 16:21:08