2017-02-16 22 views
1

私のVC内にUITableViewがあり、基本的にはタップできない最初のセクションです。しかし、このセクションの各行にUISwitchがあるので、isUserInteractionEnabled を使用することはできません。 selectionStyle.noneの設定は何も変更されません。インターフェイスインスペクタでNo Selectionを選択してこれらの行を無効にすることはできますが、テーブル全体が無効になります。私は何をすべきか?タップ時にUITableViewCellがハイライト表示される

EDIT

は、ここで次のようにあなたが.noneに最初のセクションで全てUITableViewCellsselectionStyleを設定することができ、私のカスタムセルクラス

class CustomCell: UITableViewCell { override func setHighlighted(_ highlighted: Bool, animated: Bool) { if if highlighted { self.backgroundColor = ColorConstants.onTapColor } else { self.backgroundColor = .clear } } override func setSelected(_ selected: Bool, animated: Bool) { if selected { self.backgroundColor = ColorConstants.onTapColor } else { self.backgroundColor = .clear } } }

+1

チェックを手助けするために皆

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { if indexPath.section == 0 { return false } else { return true } } 

感謝を::UITableViewCellの(質問のように)私はsetHighlightedメソッドをオーバーライドするので、私はshouldHighlightRowAt方法のように追加http://stackoverflow.com/questions/2267993/uitableview-how-to-disable-select-for-some-rows-but-other-others – Priyal

答えて

3

です:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "YOURIDENTIFIER") 

    if indexPath.section == 0 { 
     cell.selectionStyle = .none 
    } else { 
     cell.selectionStyle = .default 
    } 

    return cell 
} 

その後あなたので方法あなたはif (indexPath.section != YOURSECTION)をチェックすることができます。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if indexPath.section == 0 { 
     // DO NITHING 
    } else { 
     // DO WHATEVER YOU WANT TO DO WITH THE CELLS IN YOUR OTHER SECTIONS 
    } 
} 
+0

まだタップでハイライトされています。その理由は、selectionStyleをnoneに設定するとdidSelectRowAtの実行が回避されますが、それでもwillSelectRowAtが呼び出されます。 –

+0

理由がわかりました。カスタムUITableViewCellサブクラスでsetHighlighted関数をオーバーライドしたことを忘れてしまいました。タップした細胞の色を変えたい。私がしたことの代わりに、どうすればいいのですか?回答を更新しました –

0

あなたはコードでセル当たりの選択スタイルを設定する必要があります。 cellForRowAtで

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = table.dequeue... 

    if indexPath.section == 0 { 
     cell.selectionStyle = .none 
    } else { 
     cell.selectionStyle = .default 
    } 

    return cell 
} 
0

のでcell.selectionStyle = .none

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if(indexPath.section == desiredSection){ 
     cell.selectionStyle = .none 
     return cell; 
    } 
0

を追加し、私は.noneselectionStyleセットを持つ細胞は、タップ上で強調表示しまった理由を発見しました。私

関連する問題