2011-03-17 3 views
0

저는 IOS 프로그래밍에 대한 새 기능이고 문제가 있습니다. 나는 동적으로 cellForRowAtIndexPath 잘 작동UITableCell이 켜져있을 때 UISwitch의 상태를 확인합니다.

UISwitch *mySwitch = [[UISwitch alloc]init]; 
self.myNewSwitch = mySwitch; 
[mySwitch release]; 
[cell setAccessoryView: self.myNewSwitch]; 
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged]; 

에 다음 코드를 사용하여 각 셀에 UISwitch와로드 테이블 뷰를 가지고있다. 스위치를 클릭하면 changeState의 코드가 제대로 실행됩니다.

여기 내 문제가 있습니다. 사용자가 테이블의 셀을 누르면 스위치의 상태 (On 또는 Off)를보고 싶습니다. 내 문제는 그 didSelectRowAtIndexPath 코드에서 결정하는 방법을 알아낼 수 없다는 것입니다. 올바른 셀을 결정할 수는 있지만 Google을 사용하여 검색 할 때 해당 셀에있는 스위치에 액세스하는 방법을 찾을 수 없습니다. 다음은 didSelectRowAtIndexPath 코드입니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    //If the on/off switch is on, go to the probes page. if off, do not do anything 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    //Determine the switch state here.  
    UISwitch *theSwitch; 
    theSwitch = ??????? //<- What do I do??? 

    if (theSwitch.isOn) { 
     //define the probes view controller 
     Probes *detailViewController = [[Probes alloc] initWithNibName:@"Probes" bundle:nil]; 
     //pass in the ip address 
     detailViewController.lblIPAddr = cell.detailTextLabel.text; 

    // Pass the selected object to the new view controller. 
     [self.navigationController pushViewController:detailViewController animated:YES]; 
     [detailViewController release]; 
    } 
    //Deselect this row 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

도움이 될만한 코드가 있습니다.

-NCGrimbo

답변

1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    //If the on/off switch is on, go to the probes page. if off, do not do anything 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    //Determine the switch state here.   

    UISwitch *theSwitch = (UISwitch *)cell.accessoryView ;  

    if (theSwitch.isOn) { 
     //define the probes view controller 
     Probes *detailViewController = [[Probes alloc] initWithNibName:@"Probes" bundle:nil]; 
     //pass in the ip address 
     detailViewController.lblIPAddr = cell.detailTextLabel.text; 

    // Pass the selected object to the new view controller. 
     [self.navigationController pushViewController:detailViewController animated:YES]; 
     [detailViewController release]; 
    } 
    //Deselect this row 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

하지만 사용하는 이유를 모르겠어요,

UISwitch *mySwitch = [[UISwitch alloc]init]; 
self.myNewSwitch = mySwitch; 
[mySwitch release]; 
[cell setAccessoryView: self.myNewSwitch]; 

새로운있는 UITableViewCell 할 때마다, self.myNewSwitch는 같은 UISwitch (마지막 UiSwitch)입니다.

+0

해답을 제공해 주셔서 감사합니다. 그것은 위대한 작품. 당신이 나에게 묻는 질문에 관해서는, 이것이 내가 다른 개발자에 의해 보여 졌던 방식이다. 더 좋은 방법이 있으면 알려주세요. – NCGrimbo

관련 문제