[swift4] TableViewのタップをセルごとに無効にする方法
TableViewのタップをセルごとに無効にする方法は以下のコードを両方実装する
タップしても選択(グレー化)しないようにセルを設定
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if indexPath.row == 1 {
// セルの選択不可にする
cell.selectionStyle = UITableViewCellSelectionStyle.none
}
return cell
}
タップしてもdidSelectRowAtに行番号を渡さないように設定
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
switch indexPath.row {
case 0:
return indexPath;
// 選択不可にしたい場合は"nil"を返す
case 1:
return nil;
default:
return indexPath;
}
}