2015-12-16 16 views
1

我有一個自定義單元有一個標籤和開關,我想存儲當用戶來到該控制器時打開或關閉該開關的值。當用戶來到控制器時如何存儲開關值

var point = Int() 
func cellButtonTapped(sender: UISwitch) { 
    let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView) 
    let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable) 
    print(cellIndexPath) 
    point = cellIndexPath!.row 
    print(point) 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "setting" 
    let cell = (self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier)) as? SettingCell 
    if(indexPath.section==0) 
    { 
    let str = options1[indexPath.row] 
     cell?.label.text=str 
     cell?.delegate=self 
     return cell! 
    } 
    else if(indexPath.section==1){ 
     let str = options1[indexPath.row] 
     cell?.label.text=str 
     cell?.delegate=self 
     return cell! 
    } 
    return UITableViewCell(); 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if(section==0) 
    { 
     return options1.count; 
    } 
    else{ 
     return options2.count; 
    } 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 2; 
} 

Ccustomized細胞:

protocol MyInfo { 
    func cellButtonTapped(sender: UISwitch) 
} 
class SettingCell: UITableViewCell { 
    var delegate:MyInfo? 

    @IBAction func buttonTapped(sender: AnyObject) { 
     delegate?.cellButtonTapped(self.switchButton) 
     //print("hiii") 
    } 

    @IBOutlet weak var switchButton: UISwitch! 
    @IBOutlet weak var label: UILabel! 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 
+0

能你請讓一個更好的標題和格式化你的代碼? – manetsus

+0

我只是想reatin在自定義單元格中使用nsuserdefault –

+0

的開關狀態我修改了標題並設置了代碼格式。如果您找到更合適的標題,請再次編輯。隨意添加更多的細節來解釋你的問題到底是什麼。 – vard

回答

0

你需要一個目標添加到您的UISwitch像這樣的例子

switch.addTarget(self, action: Selector("stateChanged:"), forControlEvents: UIControlEvents.ValueChanged) 

其中stateChanged是將被調用的方法的名稱該事件被觸發(當UISwitch值改變時)。然後,你需要添加實際stateChanged方法將在NSUserDefaults

func stateChanged(switch: UISwitch) { 
    NSUserDefaults.standardUserDefaults().setBool(switch.on, forKey: "settings") 
    //The key could be another string. 
} 
0

在你cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cellIdentifier = "setting" 
let cell = (self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier)) as? SettingCell 

// bla bla cell customization 
cell.YourSwitchIn_SettingCell.tag = indexPath.row 
return cell! 
} 

保存價值在你的行動UISwitch

func cellButtonTapped(sender: UISwitch) { 
//Access switch with tag 
if sender.tag == 0 { 
    let cell = addPostTableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! SettingCell 
} 

// Do What ever you want to do with cell. 
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView) 
    let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable) 
    print(cellIndexPath) 
    point = cellIndexPath!.row 
    print(point) 

}

相關問題