我有一個表視圖,其中有複選框的項目。用戶可以選擇任何項目(多選也),他們可以按繼續按鈕付費。表視圖複選框不接受對索引0的操作
我正在使用一個按鈕作爲複選框。所以如果用戶按下按鈕,或者他們選擇了一個表格行,那麼這兩種情況都會被處理。
所以現在,我的繼續buton開始禁用。每當用戶使用didSelectRowAt
方法或複選框按鈕操作按下任何項目時,只有繼續按鈕將被啓用。
但現在,無論何時按下第一個項目或第一個複選框按鈕,我的繼續按鈕都不會啓用。如果我按第二個項目或第二個複選框,它將起作用。我不知道爲什麼。
這裏是我的didSelectRowAt
代碼和我的複選框按鈕動作代碼:
#Updated:
var selectedIndexPathArray = Array<NSIndexPath>()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemcell", for: indexPath) as! itemTableViewCell
cell.checkboxBtn.isUserInteractionEnabled = false
if selectedIndexPathArray.contains(indexPath as NSIndexPath) {
cell.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal)
proceedbutton.isUserInteractionEnabled = true
} else {
cell.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal)
proceedbutton.isUserInteractionEnabled = false
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedIndexPathArray.contains(indexPath as NSIndexPath) {
let index = selectedIndexPathArray.index(of: indexPath as NSIndexPath)
selectedIndexPathArray.remove(at: index!)
} else {
selectedIndexPathArray.append(indexPath as NSIndexPath)
}
tableView.reloadData()
}
提前感謝!
你應該在模型中包含了'selected'狀態,而不是使用一個額外的數組和斯威夫特不使用'NSIndexPath' 3+ – vadian
@vadian我不知道,我怎麼可以更新我的解決方案可以ü請更新 – david
請參閱https://stackoverflow.com/questions/46397812/how-to-make-checkmark-to-be-selected-depending-on-the-array-in-swift-3/46398755#46398755和https://stackoverflow.com/questions/44699371/how-to-save-the-state-of-the-checkbox-to-core-data-in-swift/44704524#44704524有關示例如何包含選擇信息在模型 – vadian