2013-01-31 7 views
6

選択したセルのセルビューを置き換えることはできますか?コントローラをデータソースとデリゲートとして登録しました。セルビュー、行番号、選択ステータスなどのリクエストはすべてうまくいきます。セル選択のコールバックでは、私はテーブルビューに実際のデータではなく新しいビューでセルをリロードしようとしています。基本的には、セルビューを展開して、基になるデータが選択されていればそれを表示します。問題は、私がテーブルビューに自分のコントローラに新しいビューを求めるようにする手がかりがないことです。ビューがセルの高さを要求していることはわかりますが、何も表示されません。UITableView、選択時にセルビューを置換

ビューのreloadDataを呼び出すのは効果的ですが、非効率的で、独自の問題(アニメーションの可能性、選択状態の維持など)があります。

答えて

14

内のカスタムビューに対処する必要があります

tableViewController.clearsSelectionOnViewWillAppear = NO; 

:あなたも覚えていたりして、リロード時に選択状態を忘れて、あなたのテーブルビューを伝えることができますこれを行う。

@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

どのインデックスパスが選択されたかを格納するために、NSIndexPath型のプロパティをコントローラに設定します。正確

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

。あまりにも選択を解除したいことにも注意してください。 Swiftの完全なコードはここにあります。必要に応じてcellForRowAtIndexPathのselectedIndexPathを使用します。 UITableViewController
{ 内部のvar selectedIndexPath:NSIndexPath TableViewController 輸入のUIKit

クラスSelectingTableViewControllerを選択

//? = 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

あなたが試してみました:

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

を次にあなたがリロードのためにのみ、あなたの更新のセルを指定することができます。私はかかるだろうなアプローチがあるその後、あなたもここで

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
関連する問題