2017-10-13 57 views
0

我想我的collectionView停在单元格中。问题是,当我滚动楼下,如果我释放它,它会自动在第一位置来... ...iOS:在Swift3中,在我的collectionView中,当我向下滚动时,collectionView自动位于顶部。如何阻止它?

我试着用collection.alwaysBounceVertical =真没有成功...

这里是我的代码:

override func viewDidLoad() { 
     super.viewDidLoad() 

     let layout = OrganiserLayout() 
     let frameCollection = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.width, height: self.view.frame.height + 1000) 

     let collection = UICollectionView(frame: frameCollection, collectionViewLayout: layout) 
     collection.delegate = self 
     collection.dataSource = self 
     collection.backgroundColor = UIColor.white 
     collection.register(OrganiserCollectionViewCell.self, forCellWithReuseIdentifier: "cell") 
     self.view.addSubview(collection) 
     collection.alwaysBounceVertical = true 
     collection.alwaysBounceHorizontal = true 
     collection.bounces = true 
     collection.isPagingEnabled = false 
     collection.isScrollEnabled = true 
} 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 15 
    } 

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
      switch section { 
      case 0: 
       return 40 
     } 
    } 

class OrganiserLayout:UICollectionViewLayout { 

    let cellWidth:CGFloat = 100 
    var attrDict = Dictionary<IndexPath,UICollectionViewLayoutAttributes>() 
    var contentSize = CGSize.zero 

    override var collectionViewContentSize : CGSize { 
     return self.contentSize 
    } 

    override func prepare() { 
     // Generate the attributes for each cell based on the size of the collection view and our chosen cell width 
     if let cv = collectionView { 
      let collectionViewHeight = cv.frame.height 
      let numberOfSections = cv.numberOfSections 
      self.contentSize = cv.frame.size 
      self.contentSize.width = cellWidth*CGFloat(numberOfSections) 
      for section in 0...numberOfSections-1 { 
       let numberOfItemsInSection = cv.numberOfItems(inSection: section) 
       let itemHeight = collectionViewHeight/CGFloat(numberOfItemsInSection) 
       let itemXPos = cellWidth*CGFloat(section) 
       for item in 0...numberOfItemsInSection-1 { 
        let indexPath = IndexPath(item: item, section: section) 
        let itemYPos = itemHeight*CGFloat(item) 
        let attr = UICollectionViewLayoutAttributes(forCellWith: indexPath) 
        attr.frame = CGRect(x: itemXPos, y: itemYPos, width: cellWidth, height: itemHeight) 
        attrDict[indexPath] = attr 
       } 
      } 
     } 
    } 

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { 
     // Here we return the layout attributes for cells in the current rectangle 
     var attributesInRect = [UICollectionViewLayoutAttributes]() 
     for cellAttributes in attrDict.values { 
      if rect.intersects(cellAttributes.frame) { 
       attributesInRect.append(cellAttributes) 
      } 
     } 
     return attributesInRect 
    } 

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { 
     // Here we return one attribute object for the specified indexPath 
     return attrDict[indexPath]! 
    } 


} 

class OrganiserCollectionViewCell:UICollectionViewCell { 

    var label:UILabel! 
    var seperator:UIView! 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     label = UILabel() 
     label.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(label) 

     seperator = UIView() 
     seperator.backgroundColor = UIColor.black 
     seperator.translatesAutoresizingMaskIntoConstraints = false 
     self.addSubview(seperator) 

     let views:[String:UIView] = [ 
      "label":label, 
      "sep":seperator 
     ] 

     let cons = [ 
      "V:|-20-[label]", 
      "V:[sep(1)]|", 
      "H:|[label]|", 
      "H:|[sep]|" 
     ] 

     for con in cons { 
      self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: con, options: [], metrics: nil, views: views)) 
     } 
    } 

我需要,因为我需要用户停在单元格上,然后单击它。有了这个问题,他不能点击楼下的单元格。

在此先感谢。

回答

1

设置一些高度contentSize象下面这样:

self.contentSize.height = CGFloat(2750) 

在您的代码:

class OrganiserLayout:UICollectionViewLayout { 
    override func prepare() { 
     // Generate the attributes for each cell based on the size of the collection view and our chosen cell width 
     if let cv = collectionView { 
      let collectionViewHeight = cv.frame.height 
      let numberOfSections = cv.numberOfSections 
      self.contentSize = cv.frame.size 
      self.contentSize.width = cellWidth*CGFloat(numberOfSections) 
      self.contentSize.height = CGFloat(2750) // I added this line 
      for section in 0...numberOfSections-1 { 
       let numberOfItemsInSection = cv.numberOfItems(inSection: section) 
       let itemHeight = collectionViewHeight/CGFloat(numberOfItemsInSection) 
       let itemXPos = cellWidth*CGFloat(section) 
       for item in 0...numberOfItemsInSection-1 { 
        let indexPath = IndexPath(item: item, section: section) 
        let itemYPos = itemHeight*CGFloat(item) 
        let attr = UICollectionViewLayoutAttributes(forCellWith: indexPath) 
        attr.frame = CGRect(x: itemXPos, y: itemYPos, width: cellWidth, height: itemHeight) 
        attrDict[indexPath] = attr 
       } 
      } 
     } 
    } 
} 
+0

非常感谢你,它完美的作品:d – Claudio

相关问题