2016-09-29 5 views
0

UITableViewCell의 하위 클래스 인 Prototype 셀 안에 포함 된 UISwitch이 있습니다. 이 프로토 타입 셀은 UISwitch을 포함합니다. 초기 상태는 앱이 처음 시작될 때 false로 설정하고 사용자가 변경할 때마다 상태를 저장하려고합니다. 제 문제는 UISwitch이 프로토 타입 셀 안에 포함되어있을 때 UITableViewController에서 UISwitch에 대한 참조를 얻으려고합니다. 나는 다음과 같은이 내 AppDelegate 내부Swift에서 UISwitch의 초기 상태 설정 시도

: 여기

내 관련 코드 내부

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 
    } 

} 

UITableViewController 클래스 내 프로토 타입 셀 안에 들어있는 UISwitch에 대한 참조가 필요합니다. 즉, 내 AppDelegate 클래스에 설정된대로 초기 상태가 false로 설정되며, 사용자가 해당 셀을 변경할 때마다 앞으로 상태가 유지됩니다. UISwitch의 상태. 스토리 보드에서

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() 
} 
+0

처럼 swicth가 cellforrow 방법 –

+0

어떻게 내부 상태를 설정에 액세스 할 수 있습니다 "cellForRowAtIndexPath"내부의 사용자 정의 셀 클래스에 대한 참조를 가져 옵니까?나는이 수행 할 때 셀 SwitchTableViewCell 경우 을 { ... } 나는 그것의 클래스 내부의 UISwitch 매개 변수에 대한 참조를 얻기 위해 "세포"를 사용할 수 없습니다입니다. – syedfa

+0

@syedfa 내 대답을 확인하여 셀에 대한 정보를 얻으십시오. –

답변

1

할, 예를 들어, UISwitch에 대한 태그를 설정 1. 그런 다음, viewDidLoad에, tableView.cellForRowAtIndexPath(someIndexPath)를 사용하여 셀을 얻을 :

let mySwitch = cell.viewWithTag(1) as! UISwitch 
mySwitch.on = ... 

대체 방법 :

let cell = tableView.cellForRowAtIndexPath(<insert your cell's index path here>) 

를 그런 다음, UISwitch을 얻을

  • cellForRowAtIndexPath에서 SwitchTableViewCell에 도착 셀 캐스트 , switchControl에 직접

  • 에 액세스하십시오.
  • 전체보기 컨트롤러가 양식 인 경우 Eureka을 사용하여 코드를 단순화 할 수 있습니다.

+0

업데이트 된 답변 확인 –

0

이 methpd를 사용자 지정 셀 클래스에 넣기 만하면됩니다.

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) 

아래로 셀 지금 당신은 단순히이

cell.switchControl.... // do something