2012-11-25 4 views
0

내가 가지고 UItableView 내 질문에 내 코드에서 UITableViewCell동적으로 UITableView의 UISwitch를 어떻게 변경할 수 있습니까?

의 난 OFF 다른 스위치는 이미 뷰를 만들어 내가 한 방법 난 후 하나 개의 스위치를 클릭 할 때입니다 부탁해 모든 UITableViewCellUISwitch가 포함되는 경우 ON/OFF 스위치를 사용할 수 있습니다. 그러나 OFF 다른 모든 스위치는 내 선택한 스위치를 제외하고 싶습니다.

예제 나 소스 코드 예를 통해 도움을주십시오. 베스트 감사와

편집

내 코드 :

- (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]; 
     switchview = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     cell.accessoryView = switchview; 
     switchCondition = NO; 
     [switchview setOn:NO animated:YES]; 
     [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged]; 
     [switchview release]; 
    } 
    if(switchCondition == YES){ 
    [switchview setOn:YES animated:YES]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.contentView.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[cellValueArray objectAtIndex:indexPath.row]]; 
    return cell; 
} 

- (void)updateSwitchAtIndexPath:(UISwitch*)sender { 
    if(sender.on){ 
     switchCondition = YES; 
     [table reloadData]; 
    } 
} 

답변

1

업데이트 데이터, 다음 테이블을 다시로드 테이블의 데이터 소스에서 사용 모델 (또는 가시 행 이상). 이렇게하면 각 행이 다시로드되고 각 스위치가 최신 데이터로 업데이트됩니다.

편집 :

당신은 각 스위치의 상태를 추적하는 인스턴스 변수가 필요합니다 다음 코드의 업데이트 된 버전입니다. YES 및 NO 값을 보유 할 배열을 만듭니다. 아래의 코드에서 NSMutableArray이라는 이름의 인스턴스 변수가 각 행에 YES 및 NO 값을 나타내는 NSNumber 개체로 설정되어 있다고 가정합니다. 이는 cellValueArray과 유사합니다. switchViewswitchCondition 인스턴스 변수도 제거해야합니다.

- (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; 

     UISwitchView *switch = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     cell.accessoryView = switch; 
     [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged]; 
     [switch release]; 
    } 

    UISwitchView *switch = (UISwitchView *)cell.accessoryView; 
    switch.tag = indexPath.row; // This only works if you can't insert or delete rows without a call to reloadData 
    BOOL switchState = [switchConditions[indexPath.row] boolValue]; 
    switch.on = switchState; // this shouldn't be animated 

    cell.contentView.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.text = cellValueArray[indexPath.row]; 

    return cell; 
} 

- (void)updateSwitchAtIndexPath:(UISwitch*)switch { 
    NSInteger row = switch.tag; 
    if (switch.on){ 
     // This switch is on, turn all of the rest off 
     for (NSUInteger i = 0; i < switchConditions.count; i++) { 
      switchConditions[i] = @NO; 
     } 
     switchConditions[row] = @YES; 
     [self.tableView reloadData]; 
    } else { 
     switchConditions[row] = @YES; 
    } 
} 
+0

빠른 답변 감사합니다. 내가 어떻게 할 수 있는지 이해할 수 없어서 예제를주세요. – Emon

+0

'cellForRowAtIndexPath :'에 대한 코드를 보여주세요 (질문 업데이트). 각 행에 대한 스위치 설정 방법을 보여줍니다. 또한 스위치 값이 변경 될 때 이벤트 처리에 대한 코드도 표시하십시오. – rmaddy

+0

질문이 있습니다. 지금해야 할 일을 확인하십시오. – Emon

관련 문제