Nice programing

uitableview의 업데이트 시작, 업데이트 종료 블록에서 애니메이션을 원하지 않습니까?

nicepro 2020. 9. 25. 23:36
반응형

uitableview의 업데이트 시작, 업데이트 종료 블록에서 애니메이션을 원하지 않습니까?


사용자 지정 테이블 셀을 사용하는 UITableView가 있고 각 셀에는 UIWebView가 있습니다.

UIWebView는로드하는 데 시간이 걸렸기 때문에 다시로드하지 않으려 고합니다. 어떤 상황에서는 모든 셀을로드했지만 높이가 엉망입니다. 따라서 "cellForRow"함수를 트리거하지 않고 테이블을 "릴레이 아웃"해야합니다.

  1. reloadData를 사용할 수 없습니다. 셀을 다시로드 할 것입니다.
  2. tableView.setNeedDisplay, setNeedsLayout 등을 시도했지만 아무도 테이블 셀을 재정렬 할 수 없습니다.
  3. 작동하는 유일한 방법은 beginupdates / endupdates 블록을 호출하는 것입니다.이 블록은 cellForRow를 실행하지 않고도 내 테이블을 릴레이 할 수 있습니다! 하지만 애니메이션을 원하지 않았습니다! 이 블록은 애니메이션 효과를 생성하지만 원하지 않습니다.

내 문제를 어떻게 해결할 수 있습니까?


[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

블록을 사용하는 또 다른 방법

Obj-C

[UIView performWithoutAnimation:^{
   [self.tableView beginUpdates];
   [self.tableView endUpdates];
}];

빠른

UIView.performWithoutAnimation {
    tableView.beginUpdates()
    tableView.endUpdates()   
}

내 프로젝트에서 작업하지만 일반적인 솔루션은 아닙니다.

let loc = tableView.contentOffset
UIView.performWithoutAnimation {

    tableView.reloadData()

    tableView.layoutIfNeeded()
    tableView.beginUpdates()
    tableView.endUpdates()

    tableView.layer.removeAllAnimations()
}
tableView.setContentOffset(loc, animated: true)//animation true may perform better

Swifties 이 작업을 수행하려면 다음을 수행해야했습니다.

// Sadly, this is not as simple as calling:
//      UIView.setAnimationsEnabled(false)
//      self.tableView.beginUpdates()
//      self.tableView.endUpdates()
//      UIView.setAnimationsEnabled(true)

// We need to disable the animations.
UIView.setAnimationsEnabled(false)
CATransaction.begin()

// And we also need to set the completion block,
CATransaction.setCompletionBlock { () -> Void in
    // of the animation.
    UIView.setAnimationsEnabled(true)
}

// Call the stuff we need to.
self.tableView.beginUpdates()
self.tableView.endUpdates()

// Commit the animation.
CATransaction.commit()

부드러운 전환을 선호합니다.

CGPoint offset = self.tableView.contentOffset;
[UIView transitionWithView:self.tableView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        [self.tableView reloadData];
        self.tableView.contentOffset = offset;
    } completion:nil];

시도 해봐.


섹션 5의 셀 높이를 업데이트하고 싶었고 다음 코드가 저에게 효과적이었습니다.

UiView.setAnimationsEnabled(False)
self.productTableView.reloadSections(NSIndexSet(index: SectionType.ProductDescription.hashValue), withRowAnimation: UITableViewRowAnimation.None)
self.productTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 5), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
UIView.setAnimationsEnabled(true)

참고 URL : https://stackoverflow.com/questions/9309929/i-do-not-want-animation-in-the-begin-updates-end-updates-block-for-uitableview

반응형