19

Mam UITabBarController z więcej niż 5 UITabBarItems, więc moreNavigationController jest dostępny.Jak uzyskać tekst komórki na podstawie indeksu IndexPath?

W moim UITabBarController Delegata I wykonaj następujące czynności:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
//do some stuff 
//... 

UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view; 
    moreView.delegate = self; 
} 

Chcę zaimplementować UITableViewDelegate więc mogę uchwycić wiersz, który został wybrany, należy ustawić właściwość widok niestandardowy, a następnie wcisnąć przycisk sterowania wyświetlania:

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //how can I get the text of the cell here? 
} 

Potrzebuję uzyskać tekst komórki, gdy użytkownik dotknie wiersz. Jak mogę to zrobić?

Odpowiedz

52
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     //how can I get the text of the cell here? 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     NSString *str = cell.textLabel.text; 
} 

lepszym rozwiązaniem jest utrzymanie Array komórce i używać go bezpośrednio tutaj

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

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

    Service *service = [self.nearMeArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = service.name; 
    cell.detailTextLabel.text = service.description; 
    if(![self.mutArray containsObject:cell]) 
      [self.mutArray insertObject:cell atIndex:indexPath.row]; 
    return cell; 
} 



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

    UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row]; 
    NSString *str = cell.textLabel.text; 

} 
+3

Dlaczego lepiej jest utrzymać tablicę komórce? – user592419

+0

, jeśli ponownie wywołasz funkcję cellForRowAtIndexPath, aby przechwycić wybraną komórkę, Niepotrzebne będzie wykonywanie całej funkcji i ponowne utworzenie komórki. Która byłaby kosztowna w porównaniu z przechowywaniem już utworzonej komórki w tablicy –

+0

Jeśli użyjesz insertObject Twoja tablica będzie wszędzie w jednym miejscu. Dokumenty mówią: "Jeśli indeks jest już zajęty, obiekty w indeksie i poza nim są przesuwane poprzez dodanie 1 do ich indeksów, aby zrobić miejsce.". Tak więc, kiedy cellForRowAtIndexPath jest wywoływany po raz drugi (po przewinięciu może), ta sama komórka jest wstawiana, ale oryginalna i wszystkie inne komórki są wypychane do przodu w tablicy. – amergin

Powiązane problemy