2017-09-16 81 views
0

在我的tableView中,我希望單元格是不可選的。
所以我在TableViewController寫道:Swift UITableView willSelectRowAt無法正常工作

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 
    if indexPath.section == 0 { 
     return nil 
    } else { 
     if switchIsOn! { 
      return indexPath 
     } else { 
      return nil 
     } 
    } 

} 


它的工作原理,當我使細胞正常的觸摸預期,但是當我認爲我的手指向下的細胞,它變得無論如何選擇。我怎樣才能避免這種情況?

+0

? –

+0

兩者。但對不起,我問我的問題不好,我會在一瞬間編輯它 –

+0

所以我添加到我的代碼示例,我不希望所有單元格不可選擇 –

回答

0

當開關狀態改變調用tableView.reloadData(),那麼你要選擇的動畫停止或細胞不叫didSelectRowAt在所有添加此

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    ... 

    if indexPath.section != 0 && !switchIsOn! { 
     cell.selectionStyle = .none 
    } else { 
     cell.selectionStyle = .default 
    } 
    return cell 

}