2013-07-10 60 views
78

問候, 我在讀,UITableView的默認行爲是當您滾動瀏覽部分時將部分標題行固定到表格頂部,直到下一部分將previos部分行從視圖。帶固定節標題的UITableView

我在UIViewController內有UITableView,這似乎並不是這種情況。

這僅僅是UITableViewController的瑕疵行爲嗎?

下面是一些基於我所擁有的簡化代碼。 我會顯示UIController接口和我已經實現的每個表視圖方法來創建表視圖。 我有一個幫助數據源類,可以幫助我索引我的對象與表一起使用。

@interface MyUIViewController()<UITableViewDelegate, UITableViewDataSource> 
     @property (nonatomic, readonly) UITableView *myTableView; 
     @property (nonatomic, readonly) MyCustomHelperDataSource *helperDataSource; 
    @end 

    //when section data is set, get details for each section and reload table on success 
    - (void)setSectionData:(NSArray *)sections { 
     super.sectionData = sections; //this array drives the sections 

     //get additional data for section details 
     [[RestKitService sharedClient] getSectionDetailsForSection:someId 
     success:^(RKObjectRequestOperation *operation, RKMappingResult *details) { 
      NSLog(@"Got section details data"); 
      _helperDataSource = [[MyCustomHelperDataSource alloc] initWithSections:sections andDetails:details.array]; 
      [myTableView reloadData]; 
     } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
      NSLog(@"Failed getting section details"); 
     }]; 
    } 

    #pragma mark <UITableViewDataSource, UITableViewDelegate> 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     if (!_helperDataSource) return 0; 
     return [_helperDataSource countSectionsWithDetails]; //number of section that have details rows, ignore any empty sections 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     //get the section object for the current section int 
     SectionObject *section = [_helperDataSource sectionObjectForSection:section]; 
     //return the number of details rows for the section object at this section 
     return [_helperDataSource countOfSectionDetails:section.sectionId]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     UITableViewCell * cell; 

     NSString *CellIdentifier = @"SectionDetailCell"; 

     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
      cell.textLabel.font = [UIFont systemFontOfSize:12.0f]; 
     } 

     //get the detail object for this section 
     SectionObject *section = [_helperDataSource sectionObjectForSection:indexPath.section]; 

     NSArray* detailsForSection = [_helperDataSource detailsForSection:section.sectionId] ; 
     SectionDetail *sd = (SectionDetail*)[detailsForSection objectAtIndex:indexPath.row]; 

     cell.textLabel.text = sd.displayText; 
     cell.detailTextLabel.text = sd.subText; 
     cell.detailTextLabel.textColor = [UIColor blueTextColor]; 

     return cell; 
    } 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 50.0f; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 30.0f; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section { 
    //get the section object for the current section 
    SectionObject *section = [_helperDataSource sectionObjectForSection:section]; 

    NSString *title = @"%@ (%d)"; 

    return [NSString stringWithFormat:title, section.name, [_helperDataSource countOfSectionDetails:section.sectionId]]; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 0)]; 
    header.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

    header.backgroundColor = [UIColor darkBackgroundColor]; 

    SSLabel *label = [[SSLabel alloc] initWithFrame:CGRectMake(3, 3, 260, 24)]; 
    label.font = [UIFont boldSystemFontOfSize:10.0f]; 
    label.verticalTextAlignment = SSLabelVerticalTextAlignmentMiddle; 
    label.backgroundColor = [UIColor clearColor]; 
    label.text = [self tableView:tableView titleForHeaderInSection:section]; 
    label.textColor = [UIColor whiteColor]; 
    label.shadowColor = [UIColor darkGrayColor]; 
    label.shadowOffset = CGSizeMake(1.0, 1.0); 
    [header addSubview:label]; 

    return header; 
} 
+0

這應該有效。顯示一些代碼。 –

+0

這是使用UITableView的完美方式,並且應該使用標題。你的情況是不是頭文件? – Eric

+0

@Eric頭部正在滾動行。當我滾動時,他們不停在桌子的頂部。我已經添加了我用來生成我的表格視圖的代碼。這是UIViewController中的UITableView。 – topwik

回答

233

的頭時,在表的UITableViewStyle屬性設置爲UITableViewStylePlain僅保持固定。如果您將它設置爲,則標題將向上滾動單元格。

+0

我的tableview風格是UITableViewStylePlain,但它也向上滾動單元格。 – yudun1989

+6

對此有幾種可能的解釋。一個重要的注意事項是,標題只會保留其部分內的單元格。所以,如果你在第0節有一個頭文件,它將不會在第1節的單元格中保持不變。另一個可能的解釋是你沒有用正確的樣式初始化你的UITableView。建議您使用'initWithStyle:UITableViewStylePlain',因爲像tableView.style = UITableViewStylePlain這樣的調用將不起作用。 – bachonk

+0

UITableViewStyleGrouped使頁眉/頁腳與其他單元格固定,而UITableViewStylePlain在頁眉/頁腳到達tableview的頂部/底部時使頁眉/頁腳「浮動」。 – StoneLam

1

您還可以將tableview的反彈屬性設置爲NO。這將保持節標題非浮動/靜態,但是你也失去了tableview的反彈屬性。

+0

你救了我的命!感謝Kevinl! – BurtK

0

更改的TableView風格:

self.tableview = [[UITableView的頁頭] initWithFrame:方法幀 樣式:UITableViewStyleGrouped];

作爲每對的UITableView蘋果文檔:

UITableViewStylePlain-的滑動表視圖。任何節標題或 頁腳都顯示爲內聯分隔符,並在滾動表 視圖時顯示爲浮點。

UITableViewStyleGrouped-一個表格視圖,其各節顯示不同的 行組。部分頁眉和頁腳不會浮動。

希望這個小變化將幫助你..

+1

這看起來與問題所要求的完全相反 - 它應該是'Plain',而不是'Grouped' – CupawnTae

1

雨燕3.0

的UITableViewDelegateUITableViewDataSource協議創建的ViewController。然後在裏面創建一個tableView,聲明它的風格爲UITableViewStyle.grouped。這將修復標題。

lazy var tableView: UITableView = { 
    let view = UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.grouped) 
    view.delegate = self 
    view.dataSource = self 
    view.separatorStyle = .none 
    return view 
}() 
0

現在你的tableview看起來像普通的表格樣式,但不浮動BUZ 設置表樣式設置爲組。

[_tableView setBackgroundView:nil]; 
_tableView.backgroundColor = [UIColor whiteColor];