2015-12-16 14 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) 

}

相关问题