2013-04-02 25 views
5

Mam UIViewController, że w pewnym momencie rośnie UITableView, a kiedy to robię, po prostu zainicjować zmienną instancji TableView i dodać go do widoku, ale nie jestem pewien, jak obsługiwać piszące komórki, aby dodać do widoku ; Potrzebuję identyfikatora ponownego użycia, ale nie jestem pewien, jak to ustawić.Programowe dodawanie UITableView - jak ustawić identyfikator ponownego użycia dla komórek?

Co mam zrobić w ramach tej metody?

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

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

    return cell; 
} 
+0

Powtarzasz to samo, co w jakiejkolwiek implementacji 'cellForRowAtIndexPath'. Nic o sposobie uzyskania widoku tabeli nie zmienia sposobu wyświetlania widoku tabeli. Kod, który pokazałeś, to doskonały początek. – matt

Odpowiedz

7

użyć metody initWithStyle:reuseIdentifier

  1. sprawdzić, czy cell istnieje
  2. Jeśli nie, to trzeba go zainicjować.

kod

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath*)indexPath 
{ 
    static NSString *cellIdentifier = @"wot"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    if (!cell) 
     cell = [[UITableViewCell alloc] initWithStyle: someStyle reuseIdentifier: cellIdentifier]; 

    return cell; 
} 
+0

z iOS 5, nie musisz sprawdzać, czy jest '! Komórka'. http://stackoverflow.com/questions/7946840/dequeuereusablecellwithidentifier-behavior-changed-for-prototype-cells –

+0

Ponieważ w przeciwieństwie do IB, gdzie mogę nadać wszystkim komórkom konkretny układ z UIViews na nim (UILabel, UIImage, itp.) Czy muszę utworzyć UILabel i dodać go do podglądu komórki dla każdej komórki? –

+0

Możesz utworzyć podklasę UITableViewCell i wykonać tam specyficzną konfigurację podprzestrzeni. – MJN

0

Identyfikator ponowne wykorzystanie nie musi być wyraźnie defined.In the metoda cellForRowAtIndexPath definicje zawarte w pytaniu Ci wystarczy do pracy z

dla Reference

Eg

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyIdentifier = @"MyReuseIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]]; 
    } 
    Region *region = [regions objectAtIndex:indexPath.section]; 
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row]; 
    cell.textLabel.text = timeZoneWrapper.localeName; 
    return cell; 
} 
Powiązane problemy