2012-10-25 14 views
15

Próbowałem wyszukać kilka samouczków dotyczących wypełniania widoków tabel, ale znalazłem tylko stare i nieaktualne filmy. Próbowałem zrobić to od nieco nowszego i to nie działa. MamWypełnianie komórek widoku tabeli w systemie iOS 6

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
    [chemarray addObject:@"2"]; 
    [chemarray addObject:@"test"]; 
    [chemarray addObject:@"3"]; 
    [chemarray addObject:@"science"]; 
} 

i

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [chemarray count]; 
} 

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

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

    cell.textLabel.text = [chemarray objectAtIndex:indexPath.row]; 
    return cell; 
} 

według tego poradnika, po uruchomieniu, należy wyświetlić elementy w moim tablicy, ale dla mnie to nie robi! Czy ktoś wie, jak to naprawić?

+0

ustawiłeś kontroler widoku jako delegat widoku tabeli? Czy tworzy komórkę, czy nie? –

+0

nie tworzy komórki – Alexyuiop

Odpowiedz

24

Zapomniałeś register stalówki/class dla identyfikatora komórki.

Formularz Apple dokumentacja UITableView:

Ważne: Musisz zarejestrować plik klasy lub nib przy użyciu metody registerNib:forCellReuseIdentifier: lub registerClass:forCellReuseIdentifier: przed wywołaniem metody dequeueReusableCellWithIdentifier:forIndexPath: .

Oto przykład dla zwykłego UITableViewCell:

- (void) viewDidLoad { 
    [super viewDidLoad]; 

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
} 

Jeśli nie zarejestrować stalówki/klasy metoda dequeueReusableCellWithIdentifier:forIndexPath: jest zasadniczo taka sama jak dequeueReusableCellWithIdentifier:. I nie stworzy automatycznie dla ciebie instancji.

+1

gdzie i jak tego użyć i umieścić ten – Alexyuiop

+0

Włożyć w 'viewDidLoad' – rckoenes

+0

dzięki, ale jak mam go tam umieścić? – Alexyuiop

1
  1. ustawić delegata na uitableview.delegate = self;
  2. sprawdzić, czy masz w protokole zgłoszenia <UITableViewDelegate, UITableViewDataSource>
  3. jeśli nie używać storyboard upewnij init jeśli komórka komórka == nil

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    if (cell == nil) { 
         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    
+0

Więc wywołanie dequeueReusableCell .... jest bezużyteczne, ponieważ nie "buforujesz" utworzonej komórki? –

3

Natrafiłem na ten sam problem. Jeśli używasz iOS6, to zakładam, że używasz storyboardu i skojarzyłeś kontroler widoku z twoją niestandardową klasą.

Rozwiązaniem jest przejście do serii ujęć, przejście do właściwości komórki w widoku tabeli i zmiana wartości identyfikatora na "Komórka". Nie ma potrzeby rejestrowania klasy w ViewDidLoad.

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.textLabel.text = [chemarray objectAtIndex:indexPath.row]; 
    return cell; 
} 

Sprawdź, czy masz UITableViewDataSource, UITableViewDelegate w swoim .h i podłączony tableview delegata źródło danych i cellindentifier komórka w .xib.

Powiązane problemy