はじめに
SwiftでUITableViewの特定のセルをタップ不可にする
環境
Swift 4.0.2
Xcode 9.2
方法
SwiftでUITableViewの特定のセルをタップ不可にするには willSelectRowAt でタップ不可にしたいセルをreturn nilすれば良い。
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
// row==3をタップ不可にする
if indexPath.row == 3 {
return nil
}
return indexPath
}
ただし上記だけではタップしたセルが選択状態になってしまうので cellForRowAt で合わせて選択不可も設定すればOK!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tabletestCell") as! TabletestCell
// row==3を選択不可にする
if indexPath.row == 3 {
cell.selectionStyle = UITableViewCellSelectionStyle.none
}
return cell
}