2012-03-22 38 views
21

新しいプロジェクト(Xcode 4、マスター - ディテールアプリケーション)を作成して、私が何か間違っているかどうかを確認しましたが、まだ同じ問題があります。私は、ユーザーがセルを選択解除するとき-reloadDataを呼びたいので、これは私のコードです:-didDeselectRowAtIndexPathが呼び出されないのはなぜですか?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    NSLog(@"%s", __PRETTY_FUNCTION__); 
} 

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"%s", __PRETTY_FUNCTION__); 
    [tableView reloadData]; 
} 

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"%s", __PRETTY_FUNCTION__); 
    return indexPath; 
} 

問題がdidDeselectRowAtIndexPathwillDeselectRowAtIndexPathが呼び出されていないようだということです。これは期待される行動ですか? tableView:didDeselectRowAtIndexPath:の状態

指定された行が選択解除されたことをデリゲートに伝えます。

だから私は思った通りに動作するはずです。

+0

あなたは正しくのUITableViewのためのデリゲートを設定しましたか? –

+0

デリゲートテーブルを指定しましたか?ユーザーがセルを選択できるようにしましたか? –

+0

はいとはい。選択は正常に動作しますが、選択解除が問題です。 – phi

答えて

17

tableView:willDeselectRowAtIndexPath:のドキュメントも ユーザが異なる行を選択しようとする既存の選択がある場合

この方法のみ呼び出されることを言います。デリゲートには、以前に選択された行に対してこのメ​​ソッド が送信されます。タッチダウン時にセルのハイライト表示を無効にするには、 UITableViewCellSelectionStyleNoneを使用します。

UITableViewCellSelectionStyleNoneを使用したときは機能しませんでした。

+0

これは 'willDeselectRowAtIndexPath'のためのものであり、' didDeselectRowAtIndexPath'のためのものではありません。別のセルが選択された後にのみ両方が起動されることはありますか? – phi

+0

これは両方のためのものです。[UITableViewDelegate Protocol Reference](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/)を確認してください。 – beryllium

+0

Okですが、 'willDeselectRowAtIndexPath'を忘れてしまったようです私は何か間違ったことをしていないし、 'didDeselectRowAtIndexPath'は私が望むように正しく動作していないのでしょうか? – phi

46

deselectRowAtIndexPath:animated:に電話すると、代理人方法tableView:willDeselectRowAtIndexPath:tableView:didDeselectRowAtIndexPath:メッセージは送信されません。

+4

これは受け入れられる回答である必要があります。 – tasomaniac

+0

ジェスチャ認識ツールはどうですか?'deselectRowAtIndexPath:animated:'?を使用すると実行されますか? – Ron

9

私が見つけた他の1つの変わった点は、IOS 5.1ではどんな速度であっても、テーブル上でreloadDataを呼び出すと、選択された行に対してdidDeselectRowAtIndexPathが得られないということです。私の場合は、セルのレイアウトを選択したかどうかに応じて少し調整するので、テーブルデータをリロードする前に手動でその作業を行う必要がありました。私はそれだけで単純なミスだと思うTRUE

self.tableView.allowsMultipleSelection = YES; 
+0

あなたは正しいです。 'reloadData'は、tableViewが選択された行を"忘れる "ようにします。 'reloadRowsAtIndexPaths:withAnimation:' – Philip007

0

セットallowsMultipleSelection

[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 

の代わりにその行にだけ「のtableView」を使用して: なぜあなたは、次の使用していませんか?
私は、上の行があなたに解決策を与えるだろうと確信しています!
あなたの古い質問を振り返ってみると、これがあなたを助けてくれることを願っています!
名称! :)

0

にそのテーブルビューのための

+0

を呼び出すことで、特定の行をリロードするだけです。これは問題ではないと思うのですが、tableViewはself.tableViewを参照しています。それを試して、あなたは何かを学ぶでしょう。 – phi

+0

:?大丈夫です、私が誤解した場合は、ごめんなさい!私は試して、ここにコメントとして結果を投稿します!アドバイスをありがとう! –

3

これは古い質問ですが、私はちょうど同じ問題に遭遇しました。

あなたは

[tableView reloadData] 

を使用する場合は、表データをリロードして何行が舞台裏で選択されていない - だけ

didSelectRowAtIndexPath 

が今までと呼ばれ

を意味します。私はこれがこの問題に遭遇する人に役立つことを願っています。この問題を解決するための

2

きれいな方法は、次の操作を実行することです:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) 
    // Do your cell setup work here... 

    //If your cell should be selected... 
    if cellShouldBeSelected { 
     tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None) 
    } 

    return cell 
} 

これはtableView.reloadData()コールが起こった後に選択解除されることに応答していないセルのこの全体の問題を解決します。

+0

私はSwiftが好きではありませんが、あなたのアドバイスは本当に助けてくれました。ありがとうございました! –

0

初めてセルを選択した場合、-[UITableViewDelegate tableView:didDeselectRowAtIndexPath:]メソッドは呼び出されず、-[UITableViewDelegate tableView:didSelectRowAtIndexPath:]が呼び出されます。もう1つのセルを選択した直後にdidDeselectRowAtIndexPathdidSelectRowAtIndexPathの直後に呼び出されます。

これは問題ありません。

しかし、あなたは初めに選択したセルを表示する必要がある場合、(e.q. UITableViewCellAccessoryCheckmarkを使用して)は、その後、別のセルを選択した後、あなたはおそらく、前のセルの選択を解除するために、最初に呼び出されるdidDeselectRowAtIndexPath方法をしたいです。

解決策!

-[UITableViewDataSource tableView:cellForRowAtIndexPath:]-[UITableView selectRowAtIndexPath:animated:scrollPosition:]を呼び出して、希望するセルがすでに選択されていることを通知する必要があります。

のObjective-C

#pragma mark - UITableViewDelegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    // saving the current selected row 
    SelectedRow = indexPath.row; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

#pragma mark - UITableViewDataSource 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 

    // preventing selection style 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    cell.textLabel.text = "Some text"; 

    if (indexPath.row == SelectedRow) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     // just wanted to make it selected, but it also can scroll to the selected position 
     [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
    } 

    return cell; 
} 

スウィフト3.1

// MARK: - UITableViewDelegate 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath)! 
    cell.accessoryType = UITableViewCellAccessoryType.checkmark 
    selectedRow = indexPath.row 
} 

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath)! 
    cell.accessoryType = UITableViewCellAccessoryType.none 
} 

// MARK: - UITableViewDataSource 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell") 

    // preventing selection style 
    cell.selectionStyle = UITableViewCellSelectionStyle.none 

    cell.textLabel?.text = "some text" 

    if (indexPath.row == selectedRow) { 
     cell.accessoryType = UITableViewCellAccessoryType.checkmark 
     // just wanted to make it selected, but it also can scroll to the selected position 
     tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none) 
    } 

    return cell 
} 
関連する問題