下面是一個簡化的例子。設置你的一系列物品,myItemsArray
,並給它一個isChecked
財產,這是一個bool
。然後在didSelectRow
方法中,相應地將isChecked
值切換爲true
或false
。然後,當您想要查找哪個「單元格」時,技術上myItemsArray
中的項目的值爲true
,您可以按如下所示遍歷數組。請讓我知道,如果這不清楚。
var myItemsArray = [Items]()
var checkedItemsArray = [Items]()
override func tableView(tableView:UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
if cell.accessoryType == .Checkmark {
cell.accesoryType == .None
myItemsArray[indexPath.row].isChecked == false
} else if cell.accessoryType == .None {
cell.accesoryType = .Checkmark
myItemsArray[indexPath.row].isChecked == true
}
}
func saveClicked(){
myItemsArray.removeAll()
for i in 0...myItemsArray.count-1 {
if myItemsArray[i].isChecked == true{
checkedItemsArray.append(myItemsArray[i])
}
}
}
從您的數據源獲取值,而不是視圖負責提供數據。 –
您將需要從上述數據源獲取此信息。如果嘗試迭代tableView中的所有單元格,則只會收到當時可見行的數據,因爲不可見行將從內存中出列並重用單元。 – MikeG
所以,無論你填充你的tableView,你都需要跟蹤該數組的'.Checkmark'狀態。然後,您只需遍歷數組中的具有'.Checkmark'狀態的值 – MikeG