[ios] UITableView-맨 위로 스크롤

내 테이블보기에서 맨 위로 스크롤해야합니다. 그러나 첫 번째 객체가 섹션 0, 행 0이 될 것이라고 보장 할 수는 없습니다. 테이블 뷰가 섹션 번호 5에서 시작될 수 있습니다.

그래서 전화하면 예외가 발생합니다.

[mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

테이블 뷰의 상단으로 스크롤하는 다른 방법이 있습니까?



답변

UITableView는 UIScrollView의 하위 클래스이므로 다음을 사용할 수도 있습니다.

[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

또는

[mainTableView setContentOffset:CGPointZero animated:YES];

그리고 스위프트에서 :

mainTableView.setContentOffset(CGPointZero, animated:true)

그리고 스위프트 3 이상에서 :

mainTableView.setContentOffset(.zero, animated: true)


답변

참고 :이 답변은 iOS 11 이상에서는 유효하지 않습니다.

나는 선호한다

[mainTableView setContentOffset:CGPointZero animated:YES];

테이블 뷰에 상단 삽입물이 있으면 빼야합니다.

[mainTableView setContentOffset:CGPointMake(0.0f, -mainTableView.contentInset.top) animated:YES];


답변

가능한 조치 :

1

func scrollToFirstRow() {
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}

2

func scrollToLastRow() {
    let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
    self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}

func scrollToSelectedRow() {
    let selectedRows = self.tableView.indexPathsForSelectedRows
    if let selectedRow = selectedRows?[0] as? NSIndexPath {
        self.tableView.scrollToRowAtIndexPath(selectedRow, atScrollPosition: .Middle, animated: true)
    }
}

4

func scrollToHeader() {
    self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}

5

func scrollToTop(){
    self.tableView.setContentOffset(CGPointMake(0,  UIApplication.sharedApplication().statusBarFrame.height ), animated: true)
}

맨 위로 스크롤 비활성화 :

func disableScrollsToTopPropertyOnAllSubviewsOf(view: UIView) {
    for subview in view.subviews {
        if let scrollView = subview as? UIScrollView {
            (scrollView as UIScrollView).scrollsToTop = false
        }
        self.disableScrollsToTopPropertyOnAllSubviewsOf(subview as UIView)
    }
}

요구 사항에 따라 수정하여 사용하십시오.

스위프트 4

  func scrollToFirstRow() {
    let indexPath = IndexPath(row: 0, section: 0)
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
  }


답변

NSIndexPath (빈 테이블)를 사용하지 않는 것이 좋으며 상단 포인트가 CGPointZero (콘텐츠 삽입)라고 가정하는 것이 좋습니다.

[tableView setContentOffset:CGPointMake(0.0f, -tableView.contentInset.top) animated:YES];

도움이 되었기를 바랍니다.


답변

스위프트 4 :

이것은 매우 잘 작동합니다.

//self.tableView.reloadData() if you want to use this line remember to put it before 
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)


답변

empty에서 일부 메소드를 시도하는 중 문제가 발생했습니다 tableView. 빈 테이블 뷰를 처리하는 Swift 4의 또 다른 옵션이 있습니다.

extension UITableView {
  func hasRowAtIndexPath(indexPath: IndexPath) -> Bool {
    return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
  }

  func scrollToTop(animated: Bool) {
    let indexPath = IndexPath(row: 0, section: 0)
    if self.hasRowAtIndexPath(indexPath: indexPath) {
      self.scrollToRow(at: indexPath, at: .top, animated: animated)
    }
  }
}

용법:

// from yourViewController or yourTableViewController
tableView.scrollToTop(animated: true)//or false


답변

사용 금지

 tableView.setContentOffset(.zero, animated: true)

때때로 오프셋을 잘못 설정할 수 있습니다. 예를 들어 필자의 경우 셀은 실제로 안전 영역이 삽입 된 뷰보다 약간 위에있었습니다. 안좋다.

철저한 사용

 tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)