如何解决:在Swift中使用未解析的标识符'indexPath'

时间:2017-06-07 02:35:58

标签: ios iphone swift swift3

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
}

4 个答案:

答案 0 :(得分:2)

将方法签名更正为

斯威夫特

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

目标c

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

documentation

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

<强>更新

如果这是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 :(得分: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
    }
}

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

    }
}