2017-06-07 63 views
-1
func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 1 
} 

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt cellForItemAtindexPath: IndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.CellIdentifier, forIndexPath:indexPath) as! InterestCollectionViewCell 

    cell.interest = self.interest[indexPath.item] 

    return cell 
} 
+0

你的方法沒有'indexPath' PARAM – aircraft

回答

0

改成這樣:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.CellIdentifier, forIndexPath:indexPath) as! InterestCollectionViewCell 

    cell.interest = self.interest[indexPath.item] 

    return cell 
} 

UPDATE

如果這是你還需要添加的UICollectionView功能override關鍵字之前func

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.CellIdentifier, forIndexPath:indexPath) as! InterestCollectionViewCell 

    cell.interest = self.interest[indexPath.item] 

    return cell 
} 
2

糾正你的方法簽名

斯威夫特

func collectionView(_ collectionView: UICollectionView, 
     cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

目標C

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; 

documentation

0
extension ViewController: UICollectionViewDataSource { 

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

     return interests.count 
    } 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.cellIdentifier, for: indexPath) as! InterestCollectionViewCell 

     cell.interest = interests[(indexPath as NSIndexPath).item] 

     return cell 
    } 
} 
+0

你能請格式化代碼正確嗎?看起來好像您需要將所有行縮進四個空格 – huwr

0
//MARK:- extension of collection view 

extension CreateTapUpFirstVC : UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{ 


    // MARK: - Collection View delegate & datasource 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


      return arrCategory.count 

    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TappUpCell", for: indexPath as IndexPath) as! TappUpCell 

     return cell 

    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){ 

    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
     let bounds = UIScreen.main.bounds 
     let height = bounds.size.height 

     if(height == 736) { 
      // return UIEdgeInsetsMake(top, left, bottom, right); 
      return UIEdgeInsetsMake(20, 40, 20, 40) 
     } else if(height == 667){ 
      return UIEdgeInsetsMake(20, 40, 20, 40) 
     } else if(height == 568) { 
      return UIEdgeInsetsMake(20, 20, 10, 20) 
     } else if(height == 480) { 
      return UIEdgeInsetsMake(20, 20, 10, 20) 
     } else { 
      return UIEdgeInsetsMake(20, 40, 20, 40) 
     } 
    } 


    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAt section: Int) -> CGFloat { 

     let bounds = UIScreen.main.bounds 
     // let width = bounds.size.width 
     let height = bounds.size.height 
     if(height == 736) { 
      return 10//20 
     } else if(height == 667){ 
      return 5//15 
     } else { 
      return 5//10 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     return CGSize(width: 100.0, height: 100.0); 

    } 
} 
相關問題