重置标签中的当前页码

Reset current page number in label

我使用 collectionView(在 tableView 内)用于水平滚动图像,tableView 用于垂直滚动滚动信息。

如果我向上滚动向下滚动我得到自动重置我的页码,可能是什么原因?但如果我继续滚动图像,我会得到当前页码。

我的显示当前页码的代码:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let indexPath = IndexPath(row: 0, section: 0)

    if let cell = tableView.cellForRow(at: indexPath) as? DetailTableViewCell2 {

        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)

        cell.currentPageNumber.text = "\(String(Int(pageNumber) + 1))/\(hallImages?.ImagesHalls.count ?? 0)"

    }

}

编辑: cellForRowAt 方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! DetailTableViewCell2

    cell2.currentPageNumber.text = "1/\(hallImages?.ImagesHalls.count ?? 0)"

    cell2.descriptionLabel.text = hallImages?.hallDescription

    cell2.nameLabel.text = hallImages?.studioName

    cell2.addressLabel.text = hallImages?.studioAddress

    cell2.priceLabel.text = hallImages?.hallPrice

    cell2.metrs.text = hallImages?.hallMetrs

    cell2.logoLabel.sd_setImage(with: URL(string: (hallImages?.studioLogo)!))
    cell2.logoLabel.layer.cornerRadius = 5.0
    cell2.logoLabel.clipsToBounds = true

    return cell2

}

简单的比较一下CollectionView和scrollView的实例,这样就可以计算出页码,如果是tableViewScroll则忽略计算:

 func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == collectionView {
            let indexPath = IndexPath(row: 0, section: 0)
            if let cell = tableView.cellForRow(at: indexPath) as? DetailTableViewCell2 {
                let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
                cell.currentPageNumber.text = "\(String(Int(pageNumber) + 1))/\(hallImages?.ImagesHalls.count ?? 0)"
            }
        }
    }

使用以下代码检查滚动方向并在适当的条件下添加代码

CGPoint _lastContentOffset;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  _lastContentOffset = scrollView.contentOffset;
}

  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (_lastContentOffset.x < (int)scrollView.contentOffset.x) {
      NSLog(@"Scrolled Right");
    }
    else if (_lastContentOffset.x > (int)scrollView.contentOffset.x) {
      NSLog(@"Scrolled Left");
    }

    else if (_lastContentOffset.y < scrollView.contentOffset.y) {
      NSLog(@"Scrolled Down");
    }

    else if (_lastContentOffset.y > scrollView.contentOffset.y) {
      NSLog(@"Scrolled Up");
    }
}

参考我的回答:

编辑 Swift 3

var _lastContentOffset = CGPoint.zero

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    lastContentOffset = scrollView.contentOffset
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if lastContentOffset.x < Int(scrollView.contentOffset.x) {
        print("Scrolled Right")
    }
    else if lastContentOffset.x > Int(scrollView.contentOffset.x) {
        print("Scrolled Left")
    }
    else if lastContentOffset.y < scrollView.contentOffset.y {
        print("Scrolled Down")
    }
    else if lastContentOffset.y > scrollView.contentOffset.y {
        print("Scrolled Up")
    }

}