2014-07-08 6 views
0

사용자가 일부 셀의 UISwitch 값을 업데이트 할 때 호출되는 메서드에서 사용자가 클릭 한 값을 인쇄하려고합니다. 셀의 부속품으로 UISwitch이 설정된 네 개의 셀이 있습니다. 이미 [self.basicObjects objectAtIndex:indexPath.row]을 호출하는 값과 대조 할 수있는 인프라가 있지만 문제는 어떤 셀을 눌러도 0 값이 4 개의 셀 제목을 포함하는 NSMutableArray입니다. 보조 노트로 NSIndexPath는 첫 번째 셀 값만 가져옵니다.

- (void)switchChanged:(id)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"self.basicObjects value: %@", [self.basicObjects objectAtIndex:indexPath.row]); 
} 

, 나는 비슷한 질문을보고 있었다, 그것은 또한 self.settingsTable이 4 개의 셀 섹션 1에있는로 그룹화 UITableView 것을주의하는 것이 중요 듯 (0, 1, 2). 다른 두 섹션에는 액세서리 유형으로 UISwitch이 포함되어 있지 않으며이 섹션 만이 switchChanged:cellForRowAtIndexPath: 메소드를 참조합니다.

편집 - 오후 7시 31분 당신은 당신의 액션 메소드 방식으로 indexPath를 통과 할 수

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.5]; 

    if (indexPath.section == 0) 
    { 
     // change the text to standard case 
     // change text color 
    } 

    else if (indexPath.section == 1) 
    { 
     cell.textLabel.text = [self.basicObjects objectAtIndex:indexPath.row]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     self.switchProperty = [[UISwitch alloc] initWithFrame:CGRectZero]; 
     cell.accessoryView = self.switchProperty; 

     if ([[self.basicStatus objectAtIndex:indexPath.row] isEqual:@"YES"]) 
     { 
      [self.switchProperty setOn:YES animated:YES]; 
      [self.switchProperty addTarget:self action:@selector(switchChanged:cellForRowAtIndexPath:) forControlEvents:UIControlEventValueChanged]; 
     } 

     else if ([[self.basicStatus objectAtIndex:indexPath.row] isEqual:@"NO"]) 
     { 
      [self.switchProperty setOn:NO animated:YES]; 
      [self.switchProperty addTarget:self action:@selector(switchChanged:cellForRowAtIndexPath:) forControlEvents:UIControlEventValueChanged]; 
     } 
    } 
    else if (indexPath.section == 2) 
    { 
     cell.textLabel.text = [self.objects objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [self.objectsType objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17.5]; 
     if ([[self.objectsStatus objectAtIndex:indexPath.row] isEqual:@"Setup"]) 
      cell.detailTextLabel.textColor = [UIColor colorWithRed:0.298 green:0.851 blue:0.392 alpha:1]; // #4CD964 (green) 
     else if ([[self.objectsStatus objectAtIndex:indexPath.row] isEqual:@"Not Setup"]) 
      cell.detailTextLabel.textColor = [UIColor colorWithRed:0.898 green:0.267 blue:0.267 alpha:1]; // #E54444 (red) 
    } 

    return cell; 
} 
+0

당신은 실제로 방법 있음을 호출하는 코드를 제공해야합니다. 우리에게'tableView : cellForRowAtIndexPath :'메소드를 보여주세요. – Dima

답변

0

. 보낸 사람 및 선택적으로 작업을 실행 한 이벤트 만 전달할 수 있습니다. indexPath를 다른 방식으로 가져와야합니다. 일반적인 방법 중 하나는 스위치에 indexPath.row와 동일한 태그를 지정하고 해당 태그를 배열의 인덱스로 사용하는 것입니다.

0

여기에 문제가 있습니다. 이렇게 두 번째 매개 변수를 보낼 수 없습니다.

[self.switchProperty addTarget:self action:@selector(switchChanged:cellForRowAtIndexPath:) forControlEvents:UIControlEventValueChanged]; 

다음과 같이 태그를 설정하여 사용하는 것이 좋습니다.

[self.switchProperty setTag:indexPath.row]; 
[self.switchProperty addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 

및 switchChanged에

: 것입니다 ...

- (void)switchChanged:(id)sender 
{ 
    NSLog(@"self.basicObjects value: %@", [self.basicObjects objectAtIndex:[sender tag]]); 
} 
관련 문제