2014-01-10 26 views
0

我有一個UITableViewstatic單元格,使用storyboards創建兩個部分。我使用下面的代碼來改變部分headers的外觀。UIView中的UILabel for viewForHeaderInSection不滾動表。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{  
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 


    UILabel *label = [[UILabel alloc] init]; 
    label.frame = CGRectMake(20, 1, 320, 20); 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor lightGrayColor]; 
    label.shadowColor = [UIColor grayColor]; 
    label.shadowOffset = CGSizeMake(-1.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:10]; 
    label.text = sectionTitle; 

    UIView *view = [[UIView alloc] init]; 
    [view addSubview:label]; 

    return view; 
} 

它看起來就是我想要的,但滾動tableView時,在第一頭的label不與UIView滾動。它停留在屏幕中間。至於第二個標題中的label,它的行爲就像它應該的那樣。

+0

我想你可以直接返回UILabel而不是將它包裝在UIView和As按照https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:viewForHeaderInSection:您還必須實現tableView:heightForHeaderInSection: – Piyuesh

+0

它的工作返回'UILabel',但我得到同樣的問題。謝謝。 – Jorge

+0

iOS有粘滯部分標題。因此,當您滾動時,它們在表格視圖的頂部保持可見狀態,並且只有當用戶滾動到其部分的結尾時才滾動。如果您轉到Apple的任何應用程序(例如音樂),則會在歌曲列表中看到此行爲,並在節標題中顯示字母。這有助於保持用戶的上下文。這個問題可能對你有用:http://stackoverflow.com/questions/664781/change-default-scrolling-behavior-of-uitableview-section-header –

回答

0

如果您還沒有還實施的tableView:heightForHeaderInSection:,根據它需要蘋果文檔:

返回的對象可以是一個UILabel或UIImageView的對象,以及自定義視圖。此方法僅在執行tableView:heightForHeaderInSection:時才能正常工作。

這可能會導致您的部分問題。

+0

我剛加了這個方法,但問題依然存在。謝謝。 – Jorge

+0

我可以假設你在UITableViewController中工作嗎?爲了測試它,我創建了一個新的UITableViewController,使其成爲靜態的,將表數據源委託方法註釋掉,並添加了確切的方法,並且它在沒有您遇到的問題的情況下運行。 –

0

我剛剛遇到此問題,並發現我的問題是有一個標題視圖與背景顏色設置爲clearColor。使用其他任何東西和頭文件都是正確的。