2012-08-04 4 views
-1

일부 데이터로 UITable을 만들었으며 UISwitch가 포함되어 있지만 실행시 테이블에 표시되지 않았습니다.UISwitch가 UITable에 표시되지 않음

.H

@interface CalcViewController : UITableViewController { 

    IBOutlet UITableView *mainTableView; 

    NSMutableArray *courseMURArray; 


    NSMutableArray *switchStates; 
} 

및하는 .m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    switchStates = [[NSMutableArray alloc] init ]; 

    int i = 0; 

    for (i = 0; i < 33; i++) { 
     [switchStates addObject:@"OFF"]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

     UISwitch *theSwitch = nil; 


     if (cell == nil) { 


      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 

      theSwitch = [[UISwitch alloc]initWithFrame:CGRectZero]; 
      theSwitch.tag = 100; 

      CGRect frame = theSwitch.frame; 
      frame.origin.x = 230; 
      frame.origin.y = 8; 
      theSwitch.frame = frame; 

      [theSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 


      [cell.contentView addSubview:theSwitch]; 

     }else{ 

      theSwitch = [cell.contentView viewWithTag:100]; 

     } 

     if ([[switchStates objectAtIndex:indexPath.row] isEqualToString:@"ON"]) { 
      theSwitch.on = YES; 
     }else{ 
      theSwitch.on = NO; 
     } 

return cell; 
} 

에서의

이는 선택 방법

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

    UITableViewCell *theParentCell = [[ sender superview] superview]; 
    NSIndexPath * indexPathOfSwitch = [mainTableView indexPathForCell:theParentCell]; 

    if (sender.on) { 
     [switchStates replaceObjectAtIndex:indexPathOfSwitch.row withObject:@"ON"]; 
    }else{ 
     [switchStates replaceObjectAtIndex:indexPathOfSwitch.row withObject:@"OFF"]; 
    } 


} 

모든 것이 데이터와 잘 맞지만 문제가 있습니까?

+0

버튼을 테이블 뷰 셀의 'accessoryView'로 만드는 것은 어떨까요? 그것은 훨씬 깨끗한 해결책입니다. –

+0

무슨 뜻인가요? – NamshanNet

+0

나는'cell.accessoryView = theSwitch; '를 의미한다. –

답변

1

RowAtIndexPath의 셀에 셀을 설정하면 width = height = 0 인 위치 (0,0)의 rect 인 CGRectZero로 프레임을 설정합니다. 그런 다음 위치를 재설정하지만 재설정하지 마십시오 폭은 &입니다. 당신이 개 frame.origin 선 후
추가 :

  • frame.size.width = XX;
  • frame.size.height = YY;

또는 CGRectMake (X, Y, 너비, 높이)를 사용하여 프레임을 만드십시오.

+0

나는 이것을 시도하지만 여전히 나타나지 않는다. – NamshanNet

+0

그 외의 경우, 다음을 시도하십시오. return 문에서 break를 설정 한 다음 디버거 유형에서 중단됩니다 (po [cell recursiveDescription] ). 여기에 결과를 붙여 넣으십시오. –

1

cellForRowAtIndexPath에서 셀이 nil이면 UISwitch를 초기화하고 셀에 추가합니다. 처음에 셀이 0이 아닌 경우 어떻게해야합니까?

예를 들어, UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier : @ "Cell"]이 유효한 셀을 반환하면 스위치가 전혀 생성되지 않습니다.

인터페이스 빌더의 경우 UITableViewCell의 식별자로 "셀"을 지정했는지 여부를 확인할 수 있습니다.

또는 "if (cell == nil)"외부에서 UISwitch를 초기화하는 코드를 이동하십시오. 문제가 해결되는지 확인하십시오. "if (cell == nil)"블록은 dedequeueReusableCellWithIdentifier가 nil을 반환하면 셀을 초기화하는 것으로 가정합니다.

또한 모든 스위치에 대해 태그 번호 100을 사용하고 else 블록에서 태그 100의 contentView로 theSwitch를 초기화합니다. 하나 이상의 스위치가있는 경우 iOS가 스위치에 할당해야합니까? 태그 번호를 올바르게 설정하는 방법에 대해서는 post을 참조하십시오.

관련 문제