2013-04-02 18 views
13

pojawia się następujący błąd:Dlaczego pojawia się komunikat o błędzie, że nie można usunąć kolejki, gdy mój UITableView próbuje załadować?

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier FontCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

nie jestem pewien dokładnie to, co robię źle. Ustawiłem identyfikator komórki (programowo, ponieważ nie został on utworzony przez Interface Builder) i robię wszystko, co myślałem, że powinienem robić w metodach delegatów, ale nadal otrzymuję ten błąd, gdy próbuję uzyskać ładowanie UITableView.

Oto odpowiedni kod (warto zauważyć, mam podklasy UITableViewCell dla opcji dostosowywania):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.fonts.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"FontCell"; 

    FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    if (!cell) { 
     cell = [[FontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FontCell"]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    int row = indexPath.row; 

    cell.fontFamilyLabel.text = self.fonts[row]; 

    return cell; 
} 

A oto jedyna metoda Zmieniłem w moim podklasy UITableViewCell (FontCell):

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.fontFamilyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)]; 
     self.fontFamilyLabel.textAlignment = NSTextAlignmentCenter; 

     [self.contentView addSubview:self.fontFamilyLabel]; 
    } 
    return self; 
} 

Co dokładnie robię źle?

+0

Znalazłem ten wątek to ma dobre rozwiązanie: http: // stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath –

Odpowiedz

28

Najprostszą poprawką jest po prostu zmienić ją na FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; Podobnie jak w przypadku obecnego kodu, musisz sprawdzić, czy cell nie jest nil, jeśli wykonasz tę metodę.


Alternatywnie, można zarejestrować UINib lub Class na poziomie tabeli, który jest przywiązany do @"FontCell"

Na przykład (w viewDidLoad):

[self.tableView registerClass: [FontCell class] forCellReuseIdentifier:@"FontCell"]; 

Następnie można zrobić

FontCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FontCell" forIndexPath:indexPath]; 

Przyjemnie z tą metodą jest to, że wiesz, że twoja komórka nigdy nie będzie nil, więc możesz od razu zacząć ją modyfikować.

+0

Co się stanie, jeśli zmienię identyfikator dla każdej komórki. Nie sądzę, że to zadziała, czy istnieje inny sposób? –

4

Używasz metody dequeueReusableCellWithIdentifier:forIndexPath:. Dokumentacja dla tej metody mówi tak:

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method. 

Więc

[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"]; 
+1

Prawdopodobnie powinien zarejestrować swoją klasę 'FontCell', która jest podklasą' UITableViewCell' – Brian

+0

@Brian Yeah, gdy używasz niestandardowego rejestru komórek, który to komórka –

+0

powinien być [self.tableView registerClass: [klasa FontCell] dlaCellReuseIdentifier: @ "FontCell"] ; – Henry

2

Miałem też taki problem, a rozwiązanie znalazłem to:

Przejdź do Nawigatora projektu i wybierz „ ViewController.h ". Dołącz

<UITableViewDelegate, UITableViewDataSource> 

po "UIViewController".

+0

Co to jest? Co to oznacza? – CRDave

+0

Edytowałem odpowiedź teraz. Sprawdź to. – offset

1

Jeśli używasz widoku tableview (nie kontrolera widoku tabeli), tak jak ja, nie ma komórek, które są domyślnie wyświetlane.

W serii ujęć wybrać tableview Otworzyć Atrybuty inspektor komórki Zmiana prototypowe od 0 do 1 wybrać nowo wyświetlany komórkę tabeli W Inspektorze Atrybuty ustaw Identitifier do „FontCell”

Powiązane problemy