2009-09-12 23 views
10

Potrzebuję mieć dwa UITableViews na jednym UIView. mogę pracować z jednym, oto kod:Wiele UITableViews na jednym UIView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [contentOne count]; // sets row count to number of items in array 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSString *firstValue = [[NSString alloc] initWithFormat: @"Row %i% %", indexPath.row+1 ]; 
    NSString *secondValue = [contentOne objectAtIndex:indexPath.row]; 

    NSString *cellValue = [firstValue stringByAppendingString: secondValue]; // appends two strings 

    [cell.textLabel setText:cellValue]; 



    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

Próbowałem kilka różnych sposobów. Ktoś? Gdybym mógł nazwać każdy UITableView inną nazwą, która powinna to zrobić, ale nie pozwoli mi edytować tableView na nic innego bez awarii.

+0

Duplikat Pytanie Spróbuj rozwiązanie podane w: http://stackoverflow.com/a/11789681/846372 – Soniya

Odpowiedz

28

więc trzeba w jakiś sposób powiedzieć dwa tableView S siebie - można albo ustawić „tag” nieruchomości do różnych wartości, lub mieć właściwość na kontrolerze widoku, który wskazuje na każdym widoku

@property (nonatomic, retain) IBOutlet UITableView *tableView1; 
@property (nonatomic, retain) IBOutlet UITableView *tableView2; 

następnie podłączyć je do każdego widoku interfejsu konstruktora ...

następnie w Państwa zdanie metod kontrolera można zrobić

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView == self.tableView1) { 
     return 37; 
    } else if (tableView == self.tableView2) { 
     return 19; 
    } else { 
     // shouldn't get here, use an assert to check for this if you'd like 
    } 
} 
+0

Próbowałem - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { \t jeśli (tableView == self.tableOne) { return 1; \t} \t jeśli jeszcze (tableView == self.tableTwo) { \t return 1; \t \t \t} else { \t \t zwrotną 1; \t \t} } Daje mi błąd lokalny deklarację Tableview ukrywa instancja –

+0

masz zmienną klasy o nazwie tableView? czy twoja klasa pochodzi od UITableViewController, czy coś innego? –

+0

Zrobiłem wszystko wewnątrz UIView. Nie miałem oddzielnego kontrolera dla UITableView. Właśnie wypróbowałem sekcje i jest to o wiele łatwiejsze niż próba utworzenia dwóch osobnych UITableViews. Byłem w stanie odseparować moje dwie tablice w osobne sekcje. Dziękuję za pomoc. Po prostu tego nie rozumiem. –

14

prawdopodobnie najprostszy sposób z I Uzupełnienie tego ma mieć dwie klasy delegatów i źródła danych, po jednej dla każdego widoku tabeli. Zmniejszyłoby to liczbę wystąpień if (tableview == tableview1) w kodzie kontrolera widoku.

+1

Czy możesz podać przykładową implementację tego? – dombesz

+1

, ale pomnożyłoby to liczbę implementacji metod delegowania i źródła danych. to scenariusz dawaj i bierz :) –

Powiązane problemy