2011-06-29 13 views
7

mam te metody Delegat tableview wewnątrz klasy:IOS: Tableview metody Delegat dwa tableview

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [array1 count]; 
} 

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


static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil){ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ; 
} 

cell.textLabel.text = [array1 objectAtIndex:indexPath.row]; 

return cell; 
} 

jeśli mam jeden UITableView jest ok, ale jeśli mam dwa UITableView? Jak mogę uporządkować swój kod? z tagiem?

Odpowiedz

13

Zobacz, jak wszystkie metody delegatów mają w sobie tableView:(UITableView *)tableView?

Można zdefiniować swoje poglądy tabeli w pliku nagłówka, a następnie po prostu przejść: (zakładając swoją tabelę nazywa myTable)

if (tableView == myTable) 

Wtedy można mieć wiele widoków tabel jak chcesz.

Tak na przykład:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [array1 count]; 
} 

Staje:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == myTable) 
    { 
     return [array1 count]; 
    } 
    if (tableView == myTable2) 
    { 
     return [array2 count]; 
    } 

    return 0; 
} 
0

Każda z tych metod przechodzi w odniesieniu do widoku tabeli to wszystko połączyć. Zwykle łączę każdą tabelę z gniazdem w konstruktorze interfejsów i warunkowo zwracam źródło danych na podstawie porównania z tableView w metodach delegatów i nazwach gniazd. Użycie tagu jest również możliwe, ale bardziej skomplikowane i bardziej otwarte na komplikacje podczas edytowania struktury widoku.

3

Moja sugestia polega na tym, że Twoje źródło danych działa jako delegat widoku tabeli zamiast kontrolera.

Jest to projekt bardziej zbliżony do wzorca Model-View-Controller i pozwoli na większą elastyczność i uniknięcie sprawdzania konkretnej wartości, którą argument tableView uzyskał w metodach delegatów.

W twoim przypadku twój delegat/źródło danych byłoby klasą z członkiem typu NSArray i również implementował protokół UITableViewDelegate.

1

Tak, możesz to zrobić z tagiem. Nadaj UITableViews tagi 1 i 2.

skonfigurować przełącznik:

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil){ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ; 
} 

switch ([tableView tag]) { 
    case 1:{ 
    [[cell textLabel] setText:@"First tag"] 
    break; 
    } 
    case 2:{ 
    [[cell textLabel] setText:@"Second tag"] 
    break; 
    } 
    default: 
    break; 
} 

return cell; 
} 
+0

Nicea wykorzystanie znaczników, więc dużo łatwiej obsługiwać wiele stół – Vincent