문제

나는있다 UISwitch 관습 내부 UITableViewCell (내가 부르는 서브 클래스 RootLabeledSwitchTableCell).

셀에는 a UILabel 그리고 UISwitch 서로 옆.

나는있다 @property ~라고 불리는 keychainSwitch 이 사용자 정의 셀 내부의 스위치를 가리 킵니다.

@interface RootLabeledSwitchTableCell : UITableViewCell {
    IBOutlet UILabel *textLabel;
    IBOutlet UISwitch *labeledSwitch;
}

@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISwitch *labeledSwitch;

@end

내 테이블 뷰 대의원에서 스위치 상태가 뒤집힌 경우 호출되는 선택기를 추가했습니다.

- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSString *CellIdentifier = [NSString stringWithFormat: @"%d:%d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]];
   UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       switch (indexPath.section) {
       case(kMyFirstSection): {
           switch (indexPath.row) {
               case(kMyFirstSectionFirstRow) {
                   [cellOwner loadMyNibFile:@"RootLabeledSwitchTableCell"];
                   cell = (RootLabeledSwitchTableCell *)cellOwner.cell;
                   self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];
                   [self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched) forControlEvents:UIControlEventValueChanged];
                   break;
               }
               // ...
           }
       }
       // ...
   }
}

따라서이 선택기는 올바르게 작동합니다.

- (void) keychainOptionSwitched {
   NSLog(@"Switched keychain option from %d to %d", ![self.keychainSwitch isOn], [self.keychainSwitch isOn]);
}

그러나 나는 그것을 사용할 수 없다 UISwitch 인스턴스 -setOn:animated: 초기 상태를 초기화하는 방법 :

- (void) updateInterfaceState {
   BOOL isFirstTimeRun = [[[NSUserDefaults standardUserDefaults] objectForKey:kIsFirstTimeRunKey] boolValue];
   if (isFirstTimeRun) {
      [self.keychainSwitch setOn:NO animated:NO];
   }
}

테스트에서 self.keychainSwitch ~이다 nil 원인입니다 -setOn:animated 아무것도하지 않지만 여전히 스위치와 NSLog 명령문은 스위치 상태 변경을 올바르게 인쇄합니다.

[Session started at 2009-08-24 07:04:56 -0700.]
2009-08-24 07:04:58.489 MyApp[29868:20b] keychain switch is: nil
2009-08-24 07:05:00.641 MyApp[29868:20b] Switched keychain option from 1 to 0
2009-08-24 07:05:01.536 MyApp[29868:20b] Switched keychain option from 0 to 1
2009-08-24 07:05:02.928 MyApp[29868:20b] Switched keychain option from 1 to 0

설정에 대해 놓친 것이 있습니까? self.keychainSwitch 에서 UITableView 대표 방법?

도움이 되었습니까?

해결책

나는 얼마 전에 이것을했다. Selctor를 변경해야합니다

[self.keychainSwitch addTarget:self action:@selector(keychainOptionSwitched:) forControlEvents:UIControlEventValueChanged];

그런 다음 메소드를 업데이트하십시오

-(void) keychainOptionSwitched:(id)sender {
UISwitch *tempSwitch = (UISwitch *)sender;
}

이제 Tempswitch는 변경된 스위치입니다.

다른 팁

나는 간단한 레이블로 Custom UitableViewCells와 함께 피고를 보냈고 내가 그냥 할 수 있음을 발견하기 전에 전환했습니다. uiswitch를 액세서리보기로 추가하십시오. 당신은 아마 어쨌든 이것을 알고 있으며 다른 이유로 커스텀 셀을 원하지만 이것을 언급하고 싶었던 경우를 대비하여!

다음과 같이 수행합니다 (-tableView : CellForseAtIndexPath 메서드에서) :

UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;

AccessoryView 비트는 크기와 포지셔닝을 처리하여 CGRECTZERO로 도망 갈 수 있습니다.

그런 다음 System Control 이벤트 및 Seton 메소드를 다음과 같이 사용할 수 있습니다 (참고 우리는 그것을 알고있는 Uiswitch 포인터로 캐스팅합니다).

[(UISwitch *)cell.accessoryView setOn:YES];   // Or NO, obviously!
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(mySelector)
     forControlEvents:UIControlEventValueChanged];

도움이되기를 바랍니다!

여기서 일어나는 일은 셀이 보이지 않을 때 수술을 호출하려고한다는 것입니다. 그래서 진행중인 일은 그 당시 셀이 언로드되므로 키 체인 스위치에 대한 nil을 얻는 것입니다. 다시로드 된 이후로 더 이상 틀리지 않습니다. 하지만 나는 당신이 그렇게 스위치를 할당하고 있다는 것을 알았습니다.

 self.keychainSwitch = [(RootLabeledSwitchTableCell *)cell labeledSwitch];

테이블 셀의 스위치에 대한 참조는 그것을 유지하고 있지 않기 때문에 셀이 시야를 벗어나면서 Keychainswitch가 결과적으로 NIL이되어 클래스가되기 때문에 스위치가 없어지기 때문에 스위치가 발생하기 때문입니다. 회원도 NIL이되고 있습니다. Keychainswitch를 유지 한 다음 테이블을 재구성 할 때 KEYCHAINSWITCH 또는 그 종류의 스위치를 제거 할 수 있습니다. 도움이 되었기를 바랍니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top