如何在swift中加载集合视图时设置手动滚动位置?

时间:2018-02-07 10:02:30

标签: swift uitableview uicollectionview tableheader scroll-position

由于水平滚动视图,我有主表视图,其中集合视图在表视图节标题中已确定。当我点击集合视图(第1行)表视图重新加载。但是当我点击集合视图第5行时,它会重新加载表视图并从头开始显示集合滚动位置。请帮我设置表视图内的集合视图的手动滚动位置。

以下是我正在使用的代码。

 func numberOfSections(in tableView: UITableView) -> Int
 {
    return 2
 }

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if section == 0
    {
        return 1
    }
    else
    {
        return 1
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    if indexPath.section == 0
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "banTableViewCell", for: indexPath) as! banTableViewCell

        return cell
    }
    else
    {
        if chkVal == 0
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "onepageTableViewCell", for: indexPath) as! onepageTableViewCell

            return cell
        }
        else if chkVal == 1
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "onepageTableViewCell", for: indexPath) as! onepageTableViewCell

            return cell
        }
        else if chkVal == 2
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "onepageTableViewCell", for: indexPath) as! onepageTableViewCell

            return cell
        }
        else if chkVal == 3
        {
            print("its cming gallery there")
            let cell = tableView.dequeueReusableCell(withIdentifier: "containerTableViewCell", for: indexPath) as! containerTableViewCell

            return cell
        }
        else
        {
            print("its cming calendar there")
            let cell = tableView.dequeueReusableCell(withIdentifier: "galTableViewCell", for: indexPath) as! galTableViewCell

            return cell
        }
    }
}

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
      if section == 0
      {
        let view = UIView(frame: CGRect(x:0, y:0, width:0, height:0))
        return view
      }
      else
      {
        let SegCell = tableView.dequeueReusableCell(withIdentifier: "segTableViewCell") as! segTableViewCell

        SegCell.segcol.dataSource = self as UICollectionViewDataSource
        SegCell.segcol.delegate = self as UICollectionViewDelegate
        SegCell.segcol.isPagingEnabled = true
        SegCell.segcol.isScrollEnabled = true
        SegCell.segcol.showsHorizontalScrollIndicator = true
        SegCell.segcol.register(UINib(nibName: "segCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "segCollectionViewCell")
        // Initialization code

        let indexPath = NSIndexPath(row: 3, section: 0)
        SegCell.segcol.scrollToItem(at: indexPath as IndexPath, at: UICollectionViewScrollPosition.right, animated: false)
        return SegCell
    }
}

  //Collection View Set

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
  {
     return 5
  }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "segCollectionViewCell", for: indexPath as IndexPath) as! segCollectionViewCell

    if indexPath.row == 0
    {
        cell.TextSeg.text = "FITNESS REPORT CARD"
    }
    else if indexPath.row == 1
    {
        cell.TextSeg.text = "FITNESS STATS"
    }
    else if indexPath.row == 2
    {
        cell.TextSeg.text = "DIET & NUTRITION"
    }
    else if indexPath.row == 3
    {
        cell.TextSeg.text = "GALLERY"
    }
    else if indexPath.row == 4
    {
        cell.TextSeg.text = "CALENDAR"
    }
    return cell
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize
{
    return CGSize(width:150, height:46)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    if indexPath.row == 0
    {
        tbl.reloadData()
    }
    else if indexPath.row == 1
    {
        tbl.reloadData()
    }
    else if indexPath.row == 2
    {
        tbl.reloadData()
    }
    else if indexPath.row == 3
    {
        tbl.reloadData()
    }
    else if indexPath.row == 4
    {
        tbl.reloadData()
    }
}

它没有在集合视图中设置滚动位置。

由于

0 个答案:

没有答案
相关问题