2017-10-13 1 views
0

Je voudrais ma collectionVoir pour arrêter dans la cellule. Le problème est que lorsque je fais défiler downstair, si je le libère, il vient automatiquement à la première position ...iOS: dans Swift3, dans ma collectionView, lorsque je fais défiler vers le bas, la collectionView arrive automatiquement en haut. Comment l'arrêter?

J'ai essayé avec collection.alwaysBounceVertical = true sans succès ...

Voici mon Code:

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

J'ai besoin parce que je besoin de l'utilisateur arrête sur la cellule, et cliquez dessus. Avec ce problème, il ne peut pas cliquer sur les cellules qui sont en bas.

Merci d'avance.

Répondre

1

Définir une certaine hauteur pour contentSize comme ci-dessous:

self.contentSize.height = CGFloat(2750) 

Dans votre code:

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

Merci beaucoup, cela fonctionne parfaitement: D – Claudio