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