0

테이블 셀에 슬라이더가 있으며 슬라이더를 슬라이드하면 해당 셀을 다시로드하여 셀의 업데이트 된 슬라이더 값을 표시합니다.테이블 셀의 애니메이션 제한 조건 변경

그리고 사용자가 슬라이더를 max로 이동 시켰을 때, 일부 애니메이션으로 셀의 요소의 너비 제한을 변경하고 싶습니다.

모든 것이 작동하지만 애니메이션을 볼 수 없습니다.

-(void)sliderValueChanged:(id)sender{ 

    sliderValueChanged = YES; 
    UISlider *slider = (UISlider *)sender; 

    int sliderValue; 
    sliderValue = roundf(slider.value); 
    [slider setValue:sliderValue animated:YES]; 

    NSIndexPath *indexPath = [self getIndexPathForView:slider]; 

    MySliderCell *sliderCell = (MySliderCell *)[self tableView:self.tableView cellForRowAtIndexPath:indexPath]; 

    sliderCell.sliderValueLabel.text = [NSString stringWithFormat:@"%d",sliderValue]; 

    if(sliderValue == 7){ 
     [sliderCell layoutIfNeeded]; 
     sliderCell.radioButonWidth.constant = 40; 
     [UIView animateWithDuration:2.0 
         animations:^{ 
          [sliderCell layoutIfNeeded]; 
         }]; 
    }else{ 
     [sliderCell layoutIfNeeded]; 
     sliderCell.radioButonWidth.constant = 0; 
     [UIView animateWithDuration:2.0 
         animations:^{ 
          [sliderCell layoutIfNeeded]; 
         }]; 

    } 

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

    DLog(@"slider value :%d change method called, indexpath is %@",sliderValue,indexPath); 


} 

또한 usig setNeedsUpdateConstraints 시도하고 self.tableView 대신 celllayoutIfNeeded를 호출했다. 그러나 아무 것도 효과가 없었습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까 ?

+0

다시로드 행이 셀을 다시 배치하고 애니메이션이 적용되지 않는다고 생각합니다. –

+0

reload 테이블 호출을 제거하면 셀이 슬라이더 값으로 업데이트되지 않습니다! 주변에 어떤 문제가 있습니까? –

+0

셀에서 업데이트해야 할 기타 항목이 있습니까? 이미 sliderValueLabel과 같은 값을 설정 한 것 같습니다. –

답변

1

예에서 문제는 reloadRowsAtIndexPaths을 애니메이션 블록과 동일한 루프에서 호출한다는 것입니다. 일반적으로해야 할 일은 해당 호출을 제거하면 애니메이션이 작동해야합니다.

주석 에서처럼 텍스트를 호출하지 않으면 셀에서 변경되어 슬라이더 동작 메서드에서 요청할 때 올바른 셀을 얻지 못하고있는 것 같습니다.

항상 테이블 뷰 셀의 하위 클래스에서 셀의 모든 콘센트를 사용하는 것이 좋습니다. 이 방법을 사용하면 getIndexPathForView, cellForRowAtIndexPath을 호출하지 않고 올바른 하위 뷰에 액세스 할 수 있으며 경우에 따라 인덱스 경로가 myObject = self.array[indexPath.row] 인 데이터 소스 배열에서 개체를 가져 오는 것이 좋습니다. 이렇게하면 유일한 단점은 셀에서 테이블 뷰 소유자 (일반적으로 뷰 컨트롤러)에 대한 알림을 얻는 것입니다. 이 경우 일반적으로 셀이 대리자 (뷰 컨트롤러)에 대한 약한 참조를 가지며 필요한 모든 작업에 대해 대리인을 호출하는 사용자 지정 대리자를 사용합니다.

헤더에게 : 같은 테이블 뷰 셀의 예를 제공합니다

@class MyTableViewCell; 

@protocol MyTableViewCellDelegate <NSObject> 

- (void)myTableViewCell:(MyTableViewCell *)sender changedSliderValue:(CGFloat)newValue; 

@end 

@interface MyTableViewCell : UITableViewCell 

@property (nonatomic, weak) id<MyTableViewCellDelegate> delegate; 
@property (nonatomic, strong) MyCustomObject *myObject; // The object this cell is designed to represent 


@end 

출처 :

@interface MyTableViewCell() 

@property (nonatomic, weak) IBOutlet UISlider *slider; 

@end 

@implementation MyTableViewCell 

- (void)setMyObject:(MyCustomObject *)myObject { 
    _myObject = myObject; 

    // Set any UI properties needed 
    self.slider.value = myObject.magnitude; 
} 

- (void)sliderValueChanged:(UISlider *)sender { 

    self.myObject.magnitude = sender.value; 
    [self.delegate myTableViewCell:self changedSliderValue:sender.value]; 

    // Your animation code goes here as well 

} 

@end 

그래서 당신은 당신이 다음과 같은 일을 할 것 셀을 큐에서 제거 할 때

MyTableViewCell *cell = ... 
cell.delegate = self; 
cell.myObject = self.myArray[indexPath.row]; 

그리고 셀 슬라이더가 변경되었을 때 뷰 컨트롤러 내부에 알림이 필요한 경우 위임 메서드를 구현하기 만하면됩니다.

0

[UIView animateWithDuration : animations : completion :]을 사용하고 "sliderValueChanged"메서드의 매개 변수로 완료 블록을 허용하면 블록에서 tableView를 다시로드 할 수 있으며 tableView 참조를 제거 할 수 있습니다.