重置标签中的当前页码

时间:2017-10-18 07:44:23

标签: ios swift label uipagecontrol

我使用 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)"

    }

}

currentPageNumber

编辑: 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

}

2 个答案:

答案 0 :(得分:0)

只需将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)"
            }
        }
    }

答案 1 :(得分: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");
    }
}

我的回答参考:How to detect scrollView scrolling direction?

为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")
    }

}