2017-09-15 110 views
0

林顯示動態表數據嘗試從存儲在覈心數據導致夫特到打開或關閉一個UISwitch。保存,檢索和與夫特

我加載從JSON文件中的數據,格式如下:

[{ 
    fruit = Apple; 
    id = 01; 
}, { 
    fruit = Banana; 
    id = 02; 
}, { 
    fruit = Orange; 
    id = 03; 
} 

...它是填充與數據的動態錶行。我在該行的右側有一個UISwitch的重複模板。

像這樣:

  • UISwitch 「蘋果」 狀態:OFF
  • UISwitch 「香蕉」 國家:OFF
  • UISwitch 「橙色」 狀態:OFF

我該如何去針對特定的開關打開或關閉返回的項目以開啓?

我想我需要存儲的ID,我添加到數據集的數組。例如

fruits = ["02","03"] 

...並讓代碼找到具有該ID的行嗎?我對如何去做這件事感到困惑。

我的最終結果是這樣的,當視圖加載:

  • UISwitch 「蘋果」 狀態:ON
  • UISwitch 「香蕉」 國家:ON
  • UISwitch 「橙色」 狀態:OFF

...根據用戶選擇的內容。

任何幫助將不勝感激。

回答

1

讓我們假設有這些水果的水果陣列 -

[{ 
    fruit = Apple; 
    id = 01; 
}, { 
    fruit = Banana; 
    id = 02; 
}, { 
    fruit = Orange; 
    id = 03; 
}] 

和選定的水果有這些ID陣列 -

["02","03"] 

所以,現在在你的「cellForRowAt將這些代碼片段填充錶行'方法 -

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

    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as UITableViewCell! 

    let fruitsDict = fruitsArray.object(at: indexPath.row) as! NSDictionary 

    // I've used tag for getting fruit label and fruit switch view, you can use your own custom table view cell here for that. 

    let lblFruitName = cell.contentView.viewWithTag(1) as! UILabel 
    let fruitSwitch = cell.contentView.viewWithTag(2) as! UISwitch 

    lblFruitName.text = fruitsDict.value(forKey: "fruit") as? String 

    if(selectedFruitsIDArray.contains(fruitsDict.value(forKey: "id") as? String ?? "")){ // It contains fruit id 

     fruitSwitch.setOn(true, animated: true) 

    } 
    else{ 

     fruitSwitch.setOn(false, animated: true) 
    } 

    return cell 
} 

其中fru itsArray是你所有的水果陣列和selectedFruitsIDArray選擇水果ID陣列。

我希望它會幫助你開始。

+0

謝謝!這指出我在一個偉大的方向! –

+0

不客氣。快樂編碼:) –

0

如果我們正在考慮您的數據,第二,你有沒有想過,在你的字典添加一個boolean類型的一些enable財產的每個項目裏面。因此,當用戶點擊單元格或切換此單元格時,則可以將此屬性值從false更改爲true,反之亦然。

user taps on switch 
change the enable property to true or vice versa 
change switch from off to on or vice versa 
save into coreData 

代替使用詞典,我想有一個參考類型的數據結構,諸如class,可以在其中變換字典來OBJ反之亦然。 (不是必須的,但它使您的生活更輕鬆,當您保存或更新您變換對象詞典當然)

class Item { 
    var id:Int 
    var fruit:String 
    var shouldBeEnabled:Bool 

    init(id:Int, fruit:String, enable:Bool = false) { 
     self.id = id 
     self.fruit = fruit 
     shouldBeEnabled = enable 
    } 
    // from dict to item 
    static func transform(dict:[String:Any]) -> Item { 
     var id = dict["id"] as! Int // use optional instead of explicit unwrapping 
     var fruit = dict["fruit"] as! String 
     var enabled = dict["enable"] as! Bool 
     return Item(id: id, fruit: fruit, enable: enabled) 
    } 

    // from item to dict 
    static func transform(item:Item) -> [String:Any] { 
     var dict = [String:Any]() 
     dict["id"] = item.id 
     dict["fruit"] = item.fruit 
     dict["enable"] = item.shouldBeEnabled 
     return dict 
    } 

} 

所以,下次用戶進入你的應用程序切換的狀態保存到時間啓用,那麼你填充你的tableView那麼你應該做一個檢查

consider you transform all your dict into object 
    if (data[indexPath.row].shouldBeEnabled == true) { // turn on switch } 
    else { // turn off } 

現在你的代碼有能力:保存用戶的變化,並且可以隨時加載它

+0

謝謝我會考慮實現這個選項! –