我有一个UISwitch,它包含在一个原型单元格中,它是一个UITableViewCell的子类。这个原型单元格包含一个UISwitch,它的初始状态希望在应用程序初始启动时设置为false,并且希望在用户更改它时存储它的状态。当UISwitch包含在原型单元中时,我的问题是试图从UITableViewController内部获取对UISwitch的引用。
这是我的相关代码:
在我的AppDelegate中,我有以下内容:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if !NSUserDefaults.standardUserDefaults().boolForKey("isNotFirstLaunch") {
//Set switchState to false and isNotFirstLaunch to true
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "switchState")
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isNotFirstLaunch")
//Sync NSUserDefaults
NSUserDefaults.standardUserDefaults().synchronize()
}在我的UITableViewController里面
override func viewDidLoad() {
//this line below is commented out because I don't have a reference to the UISwitch in question, but I imagine it would be something along these lines:
//switchState.on = NSUserDefaults.standardUserDefaults().boolForKey("switchState")
}
//This block of code I was able to connect to the UISwitch directly in the storyboard file, so I know this works.
@IBAction func stateChanged(withSwitchState switchState: UISwitch) {
if switchState.on {
NSUserDefaults.standardUserDefaults().setBool(switchState.on, forKey: "switchState")
} else {
NSUserDefaults.standardUserDefaults().setBool(switchState.on, forKey: "switchState")
}
}在我的课堂里,我的原型细胞:
class SwitchTableViewCell: UITableViewCell {
@IBOutlet weak var switchControl: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.switchControl.layer.cornerRadius = 16
}
}我需要一个对包含在我的原型单元格中的UISwitch的引用,在我的UITableViewController类中,这样它的初始状态被设置为false,就像在我的AppDelegate类中所建立的那样,并且每当用户更改UISwitch的状态时,它的状态就会继续保持下去。
发布于 2016-09-29 05:12:00
喜欢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SwitchTableViewCell
if !NSUserDefaults.standardUserDefaults().objectForKey("switchState")
{
cell. switchControl.setOn(true, animated: true)
}
else
{
cell. switchControl.setOn(false, animated: true)
}
cell. switchControl.tag = indexPath.row
cell. switchControl.addTarget(self, action: "switchChanged:", forControlEvents: UIControlEvents.ValueChanged)
return cell
}
func switchChanged(mySwitch: UISwitch) {
if switchState.on {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "switchState")
} else {
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "switchState")
}
self.tableview.reloadData()
}发布于 2016-09-29 06:07:26
在故事板中,为UISwitch设置一个标记,例如1。然后,在viewDidLoad中,使用tableView.cellForRowAtIndexPath(someIndexPath)获取单元格
let cell = tableView.cellForRowAtIndexPath(<insert your cell's index path here>)然后,得到UISwitch
let mySwitch = cell.viewWithTag(1) as! UISwitch
mySwitch.on = ...备选办法:
cellForRowAtIndexPath获得的单元格转换为SwitchTableViewCell,并直接访问switchControl发布于 2016-09-29 06:20:32
只需将此方法放在自定义单元格类中即可。
class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> SwitchTableViewCell {
let kSwitchTableViewCellIdentifier = "kSwitchTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "SwitchTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kSwitchTableViewCellIdentifier)
let cell = tableView.dequeueReusableCellWithIdentifier(kSwitchTableViewCellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
return cell
}在tableViewController的cellForRowAtIndexPath方法中访问单元格,如下所示
let cell = SwitchTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)现在,您可以这样简单地访问您的swicth
cell.switchControl.... // do somethinghttps://stackoverflow.com/questions/39761693
复制相似问题