2013-01-31 16 views
6

Czy można zastąpić widok komórki dla wybranej komórki? Zarejestrowałem mojego kontrolera jako źródło danych i deleguję. Żądania dotyczące widoków komórek, numerów wierszy, statusu wyboru itp. Są ładnie przedstawione. W wywołaniach wywołania komórki staram się, aby widok tabeli przeładował komórkę z nowym widokiem (nie rzeczywistymi danymi!). Zasadniczo chcę, aby widok komórki rozszerzał się i pokazywał więcej bazowych danych, jeśli został wybrany. Problem polega na tym, że nie mam pojęcia, jak ustawić widok tabeli, aby poprosić kontroler o nowy widok. Widzę, że widok żąda wysokości komórki, ale nic więcej.UITableView, zastąp widok komórki przy wyborze

Wywołanie polecenia reloadData w widoku działa, ale jest nieefektywne i zawiera zestaw własnych problemów (możliwości animacji, utrzymywanie stanu wyboru itd.).

Odpowiedz

14

Oto podejście wziąłbym do Zrób to.

@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

Ustaw właściwość typu NSIndexPath na kontrolerze, aby zapisać wybraną ścieżkę indeksu.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    self.selectedIndexPath = indexPath; 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationNone]; 
} 

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

    if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame) { 
     // Create your custom cell here and return it. 
    } 
    else { 
     // Should create a normal cell and return it. 
    } 
} 

Dokładnie. Zauważ też, że prawdopodobnie chcesz odznaczyć. Oto pełny kod w Swift. Użyj selectedIndexPath w cellForRowAtIndexPath stosownie do potrzeb.

// Wybranie TableViewController import UIKit

klasy SelectingTableViewController: UITableViewController
{ wewnętrzny selectedIndexPath var: NSIndexPath? = zero

override func viewDidLoad() 
    { 
    super.viewDidLoad() 
    tableView.estimatedRowHeight = 68.0 
    tableView.rowHeight = UITableViewAutomaticDimension 

    self.clearsSelectionOnViewWillAppear = false; 
    } 

override func tableView 
    (tableView:UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) 
     { 
     print("did select....") 

     // in fact, was this very row selected, 
     // and the user is clicking to deselect it... 
     // if you don't want "click a selected row to deselect" 
     // then on't include this clause. 
     if selectedIndexPath == indexPath 
      { 
      print("(user clicked on selected to deselect)") 
      selectedIndexPath = nil 
      tableView.reloadRowsAtIndexPaths(
       [indexPath], 
       withRowAnimation:UITableViewRowAnimation.None) 

      tableView.deselectRowAtIndexPath(indexPath, animated:false) 
      return 
      } 

     // in fact, was some other row selected?? 
     // user is changing to this row? if so, also deselect that row 
     if selectedIndexPath != nil 
      { 
      let pleaseRedrawMe = selectedIndexPath! 
      // (note that it will be drawn un-selected 
      // since we're chaging the 'selectedIndexPath' global) 
      selectedIndexPath = indexPath 
      tableView.reloadRowsAtIndexPaths(
       [pleaseRedrawMe, indexPath], 
       withRowAnimation:UITableViewRowAnimation.None) 
      return; 
      } 

     // no previous selection. 
     // simply select that new one the user just touched. 
     // note that you can not use Apple's willDeselectRowAtIndexPath 
     // functions ... because they are freaky 
     selectedIndexPath = indexPath 
     tableView.reloadRowsAtIndexPaths(
      [indexPath], 
      withRowAnimation:UITableViewRowAnimation.None) 

     } 

} 
2

Czy próbowałeś:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

Następnie można określić tylko zaktualizowaną komórkę do przeładunku. Można również powiedzieć tableview pamiętać czy zapomnieć stan selekcji na przeładowanie z:

tableViewController.clearsSelectionOnViewWillAppear = NO; 

Wtedy będziesz mieć również do czynienia z widoku niestandardowego wewnątrz

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
Powiązane problemy