2013-07-10 14 views
78

Pozdrawiam, Czytam, że domyślne zachowanie UITableView polega na przypinaniu wierszy nagłówka sekcji do górnej części tabeli podczas przewijania sekcji do momentu, aż następna sekcja wypchnie wiersz sekcji przedmio widok.UITableView ze stałymi nagłówkami sekcji

Mam UITableView wewnątrz UIViewController i wydaje się, że tak nie jest.

Czy to tylko defualtowanie dla UITableViewController?

Oto uproszczony kod na podstawie tego, co mam. Pokażę interfejs UIController i każdą metodę widoku tabeli, którą zaimplementowałem, aby utworzyć widok tabeli. Mam klasy źródło danych pomocnika, który pomaga mi indeksować moje obiekty do użytku z tabelą.

@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

To powinno zadziałać.Pokaż trochę kodu. –

+0

To jest całkiem dobry sposób używania UITableView i powinien działać z nagłówkami. Co nie robią nagłówki w twoim przypadku? – Eric

+0

@Eric nagłówki przewijają się z wierszami. Nie zatrzymują się u szczytu stołu podczas przewijania. Dodałem kod, którego używam do generowania mojego widoku tabeli. Jest to UITableView wewnątrz kontrolera UIViewController. – topwik

Odpowiedz

233

Nagłówki tylko pozostają stałe, gdy właściwość tabeli UITableViewStyle jest ustawiony na UITableViewStylePlain. Jeśli masz go ustawiony na , nagłówki będą przewijane w górę wraz z komórkami.

+0

Mój styl widoku tabeli to UITableViewStylePlain, ale przewija się też w górę z komórkami. – yudun1989

+6

Istnieje kilka możliwych wyjaśnień. Ważną informacją jest to, że nagłówki pozostaną nieruchome tylko dla komórek w obrębie jego sekcji. Tak więc, jeśli masz nagłówek w sekcji 0, to nie zostanie on naprawiony dla komórek w sekcji 1. Innym możliwym wyjaśnieniem jest to, że nie uruchamiasz swojego UITableView z odpowiednim stylem. Zalecane jest użycie 'initWithStyle: UITableViewStylePlain', ponieważ wywołanie czegoś takiego jak tableView.style = UITableViewStylePlain nie będzie działać. – bachonk

+0

UITableViewStyleGrouped powoduje, że nagłówek/stopka są poprawiane z innymi komórkami, podczas gdy UITableViewStylePlain powoduje, że nagłówek/stopka "unosi się", gdy nagłówek/stopka osiąga górę/dół widoku tabeli. – StoneLam

1

Można także ustawić właściwość odrzuceń tableview na NIE. Spowoduje to, że nagłówki sekcji nie będą pływające/statyczne, ale utracisz także właściwość Odbicie widoku tabeli.

+0

Uratowałeś mi życie! Dzięki Kevinl! – BurtK

0

Zmień Tableview Styl:

self.tableview = [[UITableView alloc] initwithFrame: Rama styl: UITableViewStyleGrouped];

Zgodnie dokumentacji jabłkowego UITableView:

UITableViewStylePlain- zwykły widok tabeli. Dowolne nagłówki sekcji lub stopki są wyświetlane jako wbudowane separatory i unoszą się, gdy przewijany jest widok tabeli o numer .

UITableViewStyleGrouped- Widok tabeli, którego sekcje przedstawiają odrębne grupy wierszy. Nagłówki i stopki sekcji nie są unoszone.

Nadzieja ta mała zmiana pomoże ..

+1

wydaje się to być dokładnie przeciwne do tego, o co pytano - powinno być "Zwykłe", a nie "Zgrupowane" – CupawnTae

1

Swift 3.0

Tworzenie ViewController z UITableViewDelegate i UITableViewDataSource protokołów. Następnie utwórz w niej tabelę, deklarując jej styl jako UITableViewStyle.grouped. Naprawi to nagłówki.

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

Teraz twój tableview wygląda jak zwykły styl tabeli, ale nie unoszą Buz ustawiania stylu tabeli zamieszczonej na grupy.

[_tableView setBackgroundView:nil]; 
_tableView.backgroundColor = [UIColor whiteColor]; 
Powiązane problemy