2013-03-06 3 views
0

슬라이더를 하위 뷰로 추가 한 tableView가 있습니다. 나는 이것을 두 가지 방법으로 시도한다.UITableView에서 테이블 다시로드하기

방법 항목 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 

    UISlider* theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease]; 
    theSlider.maximumValue=99; 
    theSlider.minimumValue=0; 
    [cell.contentView addsubview:theSlider]; 
    return cell; 
} 

문제 : 내가 이것을 사용하는 경우, 난 테이블을 이동하면 다음, 슬라이더가 &를 다시 가져 슬라이드 값보다는 나에게 최소 값을 보여줍니다. 그래서 두 번째 방법을 사용합니다.

방법 2 :이 문제

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier=[NSString stringWithFormat:@"CellIdentifier%d",indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
     UISlider* theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease]; 
     theSlider.maximumValue=99; 
     theSlider.minimumValue=0; 
     [cell addSubview:theSlider]; 
    } 
    return cell; 
} 

: 나는 그것에 탭으로 시도 내 view.When에서 다음 버튼을, 나는 최소한으로 모든 슬라이더 (즉, 설정 슬라이더 값을 다시로드하려면). 테이블을 다시로드하려고하지만 원하는 결과를 얻지 못했습니다. 당신의 버튼 콜이 줄에서 확인

+0

[cell.contentView addSubview : theSlider]와 함께 메소드 -2를 사용하십시오. 그리고 follo Vishal의 ans. :) – iPatel

+0

나는 또한 그 – user1673099

+0

너 슬라이더에 대한 값을 설정해야합니다보십시오. 나는 당신이 가치를 더하고있는 곳을 보지 못한다. 최소 및 최대 값만 설정하고 있습니다. –

답변

0

는 사용자가 추가 한 모든 데이터 테이블보기를 다시로드합니다 :

여기
[[self mytableview] reloadData]; 

, mytableview 테이블보기의 obj를 당신이 당신의 테이블 뷰 객체를 넣어.

+0

답장을 보내 주셔서 감사합니다! 하지만 작동하지 않습니다. – user1673099

+0

두 번째 방법을 사용할 때 셀에 슬라이더 표시 여부? – Vishal

+0

예, 정상적으로 작동합니다. – user1673099

2

사용자가 슬라이더를 변경하면 마지막 값을 추적해야합니다. 그런 다음 cellForRowAtIndexPath:에서 슬라이더의 값을 마지막으로 저장된 값으로 설정해야합니다.

는 슬라이더의 가치를 유지하기 위해 인스턴스 변수를 추가

NSMutableDictionary *_sliderValues; // don't forget to initialize this variable 

// Helper method to located the slider in the cell 
- (UISlider *)findSlider:(UIView *)view { 
    for (UIView *subview in view.subviews) { 
     if ([subview isKindOfClass:[UISlider class]]) { 
      return (UISlider *)subview; 
     } else { 
      UISlider *slider = [self findSlider:subview]; 
      if (slider) { 
       return slider; 
      } 
    } 

    return nil; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
     UISlider *theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease]; 
     theSlider.maximumValue = 99; 
     theSlider.minimumValue = 0; 
     [theSlider addTarget:self action:@selector(sliderChanged:) forControlEvents: UIControlEventValueChanged]; 
     [cell.contentView addSubview:theSlider]; 
    } 

    UISlider *slider = [self findSlider:cell]; 
    slider.tag = indexPath.row 
    slider.value = [_sliderValue[@(slider.tag)] floatValue]; 

    return cell; 
} 

// Process changes to a slider 
- (void)sliderChanged:(UISlider *)slider { 
    NSInteger tag = slider.tag; 
    [_sliderValues setObject:@(slider.value) forKey:@(tag)]; 
} 

// called when the Next button is tapped 
- (void)nextAction { 
    [_sliderValues removeAllObjects]; 
    [self.tableView reloadData]; 
} 

참고 :이 컴파일이나 실행되지 않았습니다. 오타가있을 수 있습니다.

+0

좋습니다. 테이블이 스크롤 될 때 슬라이더의 값을 추적하려면이 코드가 필요합니다. 다시로드 버튼을 처리하려면 저장된 값을 지우고 테이블을 다시로드해야합니다. – rmaddy

+0

@ user1673099 방금 내 대답을'nextAction'으로 업데이트합니다. – rmaddy

+0

죄송합니다, 무슨 뜻인지 이해가 안됩니다. 내 코드의 목적은 테이블을 스크롤하면 슬라이더 값이 원래 위치에 유지되도록하는 것입니다. 다음 버튼을 누를 때만 슬라이더 값을 재설정해야합니다. – rmaddy

0

다음 버튼을 클릭하십시오. 모든 슬라이더를 tableview 안에 넣고 0으로 재설정하십시오.

for (UISlider *but in [self.view.subviews subviews]) // or use [yourtableview subviews] 
    { 
     if ([but isKindOfClass:[UISlider class]]) { 
      [but setValue:0.0]; 
     } 
    } 
관련 문제