2017-03-18 98 views
1

我剛開始學習Xcode和Swift,這是我在堆棧溢出中的第一個問題。所以請原諒我,如果我問一些微不足道的東西,或者我的問題是以不恰當的方式定義的。UITableView自定義單元格選項無法修改源變量

所以我的問題是以下幾點: 我想創建一個設置視圖,使用自定義單元格的TableView。 我有兩種類型的單元格:ValueCell(帶有兩個標籤)和SwitchCell(一個標籤和一個開關)。我有兩個單元的課程。我的UITableViewController包含填充tableview的輸入變量。我可以在一些掙扎之後管理。所以我的TableView填充了我所有的設置。 所以問題是以下幾點: 當我與它交互時,如何讓Switch修改原始值?

非常感謝您的幫助。

我SwitchCell類:

import Foundation 
import UIKit 

class SwitchCell: UITableViewCell { 
    //SettingCell with switch 
    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var value: UISwitch! 

    // I TRIED TO MAKE IT WORK, THIS IS CONNECTED TO THE SWITCH IN STORYBOARD 
    @IBAction func switchStateChanged(_ sender: UISwitch) { 
    if self.value.isOn { 
     print("\((self.name.text)!)") 
     print(self.value.isOn) 
    } else { 
     print("\((self.name.text)!)") 
     print(self.value.isOn) 
     } 
    } 

} 

這裏是BoolSetting類:

class BoolSetting: Setting { 
var value: Bool 
init (name: String, value: Bool) { 
    self.value = value 
    super.init(name: name, type: .Bool) 
} 
} 

我SettingsListItems類:

class SettingsListItems: ListItems { 
override init(){ 
    super.init() 



    //MARK: Settings data structure 

    addSection(section: "Image options", item: [ 
     IntSetting(name: "Resolution (dpi)", value: 1024), 
     IntSetting(name: "Resolution quality", value: 8), 
     IntSetting(name: "Zoom", value: 100) 
     ]) 

    addSection(section: "Capture options", item: [ 
     BoolSetting(name: "Use flash", value:true), 
     IntSetting(name: "Number of images to take", value: 2), 
     FloatSetting(name: "Zero width (inch)", value: 0.405000), 
     IntSetting(name: "Zero width (pixel)", value: 414), 
     IntSetting(name: "Zero offset x (pixel)", value: 0), 
     IntSetting(name: "Zero offset y (pixel)", value: -500) 
     ]) 
    addSection(section: "Debug", item: [ 
     BoolSetting(name: "Debug mode", value: false), 
     BoolSetting(name: "Save captured image", value: false) 
     ]) 
} 
} 

最後這裏是SettingsViewController類:

import UIKit 

class SettingsViewController: UITableViewController { 
let mySettings = SettingsListItems() 



override func viewDidLoad() { 
    super.viewDidLoad() 


    // Do any additional setup after loading the view. 

} 

override func viewDidDisappear(_ animated: Bool) { 
    super.viewDidDisappear(true) 

    //Csak ellenőrzés, hogy változnak-e a setting értékek 
    for item in mySettings.items { 
     for s in item{ 
      switch s.type{ 
      case .Bool: 
       print((s as! BoolSetting).value) 
      case .Int: 
       print((s as! IntSetting).value) 
      case .Float: 
       print((s as! FloatSetting).value) 
      } 

     } 
    } 
} 



//hány section legyen a TableViewban 
override func numberOfSections(in tableView: UITableView) -> Int { 
    return mySettings.sections.count 
} 

//Hány row legyen egy section-ön belül 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return mySettings.items[section].count 
} 

//Mi legyen a cellák tartalma 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell: UITableViewCell 

    //egyszerűsítő declarations 
    let row = indexPath.row 
    let section = indexPath.section 
    let mySetting = mySettings.items[section] [row] 

    //Setting type választó 
    switch mySetting.type{ 
    case .Bool: 
     cell = tableView.dequeueReusableCell(withIdentifier: "switchCell", for: indexPath) 
     (cell as! SwitchCell).name.text = (mySetting as! BoolSetting).name 
     (cell as! SwitchCell).value.isOn = (mySetting as! BoolSetting).value 

//This was what I tried to do, but I think something is very wrong here 
     (cell as! SwitchCell).switchStateChanged(cell as! SwitchCell.value) { 
      (mySetting as! BoolSetting).value = (cell as! SwitchCell).value.isOn 
     } 


    case .Int: 
     cell = tableView.dequeueReusableCell(withIdentifier: "valueCell", for: indexPath) 
     (cell as! ValueCell).name.text = (mySetting as! IntSetting).name 
     (cell as! ValueCell).value.text = String((mySetting as! IntSetting).value) 
    case .Float: 
     cell = tableView.dequeueReusableCell(withIdentifier: "valueCell", for: indexPath) 
     (cell as! ValueCell).name.text = (mySetting as! FloatSetting).name 


    } 
    return cell 
} 

//Section címek megadása 
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return mySettings.sections[section] 
} 


} 
在SwitchCell類

回答

0

聲明一個屬性:在switchStateChanged方法底部

var switchBlock : ((Bool) ->())? = nil 

做到這一點:

self.switchBlock?(self.value.isOn) 

終於在cellForRow替換此:

(cell as! SwitchCell).switchStateChanged(cell as! SwitchCell.value) { 
     (mySetting as! BoolSetting).value = (cell as! SwitchCell).value.isOn 
    } 

與這個:

cell.switchBlock = { isOn in (mySetting as! BoolSetting).value = isOn} 

利潤!

+0

親愛的安德烈,感謝您的快速回復!它工作得很好。 –