2013-01-31 15 views
6

È possibile sostituire una vista cella per la cella selezionata? Ho registrato il mio controller come origine dati e delegato. Le richieste di visualizzazioni di celle, numeri di riga, stato di selezione, ecc. Sono tutte ben presentate. Nei callback di selezione delle celle, sto provando ad avere la vista tabella ricaricare la cella con una nuova vista (non i dati reali!). Fondamentalmente, voglio che la vista cella si espanda e mostri più dati sottostanti, se è selezionata. Il problema è che non ho idea di come fare in modo che la vista tabella chieda al mio controller una nuova vista. Vedo che la vista richiede l'altezza della cella, ma niente di più.UITableView, sostituire la visualizzazione cella sulla selezione

Chiamare reloadData sulla vista funziona, ma è inefficiente e viene fornito con un set di problemi (possibilità di animazione, mantenimento dello stato di selezione, ecc.).

risposta

14

Ecco l'approccio avrei preso a Fai questo.

@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

impostare una proprietà di tipo NSIndexPath per il controller per memorizzare gli percorso dell'indice è stato selezionato.

- (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. 
    } 
} 

Esattamente

. Nota anche che probabilmente vuoi deselezionare. Ecco il codice completo in Swift. Utilizzare selectedIndexPath in cellForRowAtIndexPath come appropriato.

// Selezione TableViewController importazione UIKit

classe SelectingTableViewController: UITableViewController
{ interna var selectedIndexPath: NSIndexPath? = nil

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

Hai provato:

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

Quindi è possibile specificare solo il tuo cellulare aggiornato per la ricarica. Si può anche dire la vostra Tableview da ricordare o dimenticare lo stato di selezione su di ricarica con:

tableViewController.clearsSelectionOnViewWillAppear = NO; 

Poi si avrà anche a che fare con la visualizzazione personalizzata all'interno

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
Problemi correlati