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
}
}
能你请让一个更好的标题和格式化你的代码? – manetsus
我只是想reatin在自定义单元格中使用nsuserdefault –
的开关状态我修改了标题并设置了代码格式。如果您找到更合适的标题,请再次编辑。随意添加更多的细节来解释你的问题到底是什么。 – vard