2011-11-17 5 views
2

에있는 경우 다음 accesoryView을 클릭을 얻을 UISwitch합니다스위치가 나는이처럼 보이는있는 UITableViewCell 구축을 위해 노력하고

나는 아직 이미지를 게시 할 수 없기 때문에 내가 말하여 설명하려고합니다 UISwitch (가운데)와 액세서리 (오른쪽)가있는 레이블 (왼쪽)입니다.

아이디어는 스위치가 꺼져 있으면 accessoryView를 볼 수 있지만 비활성화되어 있는지입니다 ... 가주 사진을 찍어 바랍니다. 사용자가 스위치를 켜면 오른쪽을 탭하여 탐색하여 선택할 수있는 옵션 목록을 볼 수 있습니다. 문제는 스위치가 도청 될 때 셀이 스위치가 아닌 탭을 얻는 것입니다.

내가해야 할 일은 무엇입니까? (스위치가 탭을 먼저 얻도록). 나는 그것이 첫 번째 레스 폰더 일이라고 추측하지만 나는 필요한 마술 코드를 찾지 못하고있다. 나는이 과거 일단

아마 액세서리 내 자신의 활성화/비활성화 ...

감사를 알아낼 수 있습니다.

답변

1

UISwitch 컨트롤을 만들고 셀 내용보기에 추가하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      cell.accessoryType = (UITableViewCellAccessoryNone; 
      UISwitch* aSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
aSwitch.tag = [indexPath row]; 

      CGRect rect = cell.frame; 
      CGRect switchRect = aSwitch.frame; 
      switchRect.origin = CGPointMake((rect.size.width/2) - (aSwitch.frame.size.width/2), 
               (rect.size.height/2) - (aSwitch.frame.size.height/2)); 

      aSwitch.frame = switchRect; 
      [aSwitch addTarget:self action:@selector(switchSwitched) forControlEvents:UIControlEventValueChanged]; 
      [cell addSubview:aSwitch]; 


      [aSwitch release]; 
     } 

     return cell; 
    } 

    - (void)switchSwitched:(UISwitch*)sender { 

     if (sender.on) { 

    UITableViewCell* aCell = [self.tableView cellForRowAtIndexPath: 
           [NSIndexPath indexPathForRow:sender.tag inSection:0]]; 

    aCell.accessoryType = (sender.on == YES) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone; 
     } 
    } 

또한 서브 클래 싱있는 UITableViewCell 및 추가있는 UITableViewCell의 nib 파일에 의해 다른이를 구현할 수 있습니다. UIViewTableController를 Cell nib 파일의 파일 소유자로 만들고, 서브 클래 싱 된 셀에 대한 IBOutlet을 UIViewController에 추가하십시오.

 [[NSBundle mainBundle] loadNibNamed:@"Your Custom Cell nib file name" owner:self options:nil]; 

아이폰 OS http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7 애플의 프로그래밍 가이드를 참조하여 사용자 정의 셀을로드
관련 문제