2017-08-01 3 views
-1

テーブルビューに問題がありましたdidSelectメソッドとprepareForSegueです。私はSWRevealControllerを私のアプリで使用しました。セルを選択すると、ビューが表示されます。 ときどき正しく動作しない。ビューを表示するには2回のタップが必要です。数ヶ月前に私は、ブロックアクションを実行する古い公開画面フレームを使用しました。それは完全に働いた。テーブルビューのセルには、ビューを表示するために2つのタップが必要です

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    for (int i=0; i<6; i++) 
    { 
     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     if (i == indexPath.row) 
     { 
      cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRedSelected green:KColorGreenSelected blue:KColorBlueSelected alpha:1]; 
     } 
     else 
     { 
      cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRed green:KColorGreen blue:KColorBlue alpha:1]; 
     } 
    } 
} 
+0

あなたは 'didSelect'コード –

+0

を表示できますか?for(int i = 0; i <6; i ++)を使用しました。 –

+0

セルの色を変更します。 – phani

答えて

0

問題は、あなたが(それは標準の動作なので、私は、仮定)再利用可能なセルをデキューデリゲートメソッドを、呼び出している。このラインで

[self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

です。再利用可能なセルをデキューしたくない場合は、現在表示されているセルで何かをしたいとします(indexPath)。 UITableView

[tableView cellForRowAtIndexPath:indexPath];

完全なコードからその利用方法を行うには

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // grab the selected cell and give it selected color 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRedSelected green:KColorGreenSelected blue:KColorBlueSelected alpha:1]; 
} 

- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    // grab the deselected cell (if it's still visible) and give it deselected color 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    cell.contentView.backgroundColor = [UIColor colorWithRed:KColorRed green:KColorGreen blue:KColorBlue alpha:1]; 
} 
あなたは一度設定した色がセルに滞在するので、あなたはまた、 UITableViewDelegate方法 cellForRowAtIndexPathに適切な色を設定する必要があります

それが再利用されたとき。

+0

ちょうど今私はこれを試みた。これも機能していません。 – phani

+0

答えを編集しました。今すぐ試してください。あなたは物事を複雑にしています。あなたがする必要があるのは、 'didSelect'で1つのセルをつかんで、選択した色を与え、' didDeselect'で1つのセルをつかんで、選択解除します。 –

+0

また、 'UITableViewDelegate'メソッド' cellForRowAtIndexPath'で、セルが選択されているかどうかを確認し、適切な色を付ける必要があります。 –

-1

これは、セル

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 

、同様のループには使用しないでクリックされた取得するために完全に間違っている方法です。この代わりに使用する必要があります

UITableViewCell *cell = [tableView cellForIndexPath:indexPath]; 

正しいセルを取得します。

1

私のために動作することをdidSelectRowAtIndexPathの内側とdidDeselectRowAtIndexPath機能

dispatch_async(dispatch_get_main_queue(), ^{ 

    //Write code what you need 

    }); 

このコードを追加します。

関連する問題