【问题标题】:UICollectionView cannot find memberUICollectionView 找不到成员
【发布时间】:2014-08-28 16:00:11
【问题描述】:

我正在尝试以编程方式在 Swift 中创建 UICollectionView。 我可以设置它,但是一旦我尝试在 self.collectionView 上使用更复杂的操作,例如 insertItemsAtIndexPath 或 deleteItemsAtIndexPath,我就会收到“找不到成员”错误。更简单的操作,例如 numberOfSections (如我的示例代码所示)可以正常工作。我已经将我的代码与许多其他代码进行了比较,但找不到问题。

MainViewController

import UIKit
import CoreMotion
import MediaPlayer
import SpriteKit

class MainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


    var containerView : UIView?        
    var collectionView : UICollectionView?        

    var card : UIView?
    var fishArray = NSMutableArray()

    var cellCount = 20

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.blackColor()


        ////// COLLECTION VIEW

        var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tappiDiTap:")

        var layout = FishFlowLayout()
        collectionView = UICollectionView(frame: CGRectMake(96/2,352/2,1857/2,1117/2), collectionViewLayout: layout)
        collectionView!.delegate = self
        collectionView!.dataSource = self
        collectionView!.addGestureRecognizer(tapGestureRecognizer)
        collectionView!.registerClass(FishCollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
        self.view.insertSubview(collectionView!, belowSubview:scaleBackground!)

        for i in 0..9 {
            fishArray.addObject("Fish: \(i)")
        }


    }


    func tappiDiTap(tapGestureRecognizer : UITapGestureRecognizer) {
        if (tapGestureRecognizer.state == .Ended) {
            var initialPinchPoint : CGPoint = tapGestureRecognizer.locationInView(self.collectionView)
            var tappedCellPath : NSIndexPath = self.collectionView!.indexPathForItemAtPoint(initialPinchPoint)

            if (tappedCellPath==nil) {
                self.cellCount = self.cellCount+1
                self.collectionView!.backgroundColor = UIColor.blackColor()
            } else  {
                self.collectionView!.backgroundColor = UIColor.blueColor()
                println(slef.collectionView!.numberOfSections)
            }
        }
    }

    //////////////////COLLECTIONVIEW DELEGATE METHODS

    func collectionView(collectionView : UICollectionView!, numberOfItemsInSection section: Int) -> Int {
        return cellCount
    }

    func collectionView(collectionView : UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellID", forIndexPath: indexPath) as FishCollectionViewCell
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

鱼流布局

import UIKit

class FishFlowLayout: UICollectionViewFlowLayout {

    init() {
        super.init()

        self.scrollDirection = UICollectionViewScrollDirection.Vertical
        self.itemSize = CGSizeMake(220,270)
       self.minimumLineSpacing = 18
        self.sectionInset = UIEdgeInsetsMake(0,0,0,0)
    }

}

FishCollectionViewCell

import UIKit

class FishCollectionViewCell: UICollectionViewCell {

    init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 5

        var card = Card(frame: self.bounds)
        self.addSubview(card)
    }

}

【问题讨论】:

  • 您能否说明您尝试拨打这些电话的位置并准确告诉我们您遇到了什么错误?
  • 我尝试在 tapGestureRecognizer 中实现 deleteItemsAtIndexPath,这样当某物被点击时(或者更好的是被点击的单元格)应该消失。也许一些如何执行 deleteItemsAtPathIndex 的示例代码会有所帮助,或者在 Swift 中一些更高级的 UICollectionView 实现会很有帮助,因为我在 XCode 方面没有那么丰富的经验。

标签: ios xcode ios7 swift uicollectionview


【解决方案1】:

实现deleteItemsAtIndexPathinsertItemsAtIndexPath 的关键是您需要确保collectionView:numberOfItemsInSection: 在删除或插入后返回正确数量的项目。例如,如果表当前有 10 个元素,而您调用 deleteItemsAtIndexPath,则需要确保下次集合视图询问元素数量时,它返回 9,否则会崩溃。集合视图不会为您处理增加或减少该数字。

【讨论】:

    【解决方案2】:

    您的问题可能是没有解开可选值。

    例如尝试这样做

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
        switch type {
        case .Insert:
            self.collectionView.insertItemsAtIndexPaths([newIndexPath])
        ...etc
    
    }
    

    会给你错误could not find member 'insertItemsAtIndexPaths'

    它不能。该方法需要[AnyObject] 而不是[Optional]

    修复解开 Optional 的问题。

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
            switch type {
            case .Insert:
                self.collectionView.insertItemsAtIndexPaths([newIndexPath!])
            ...etc
    
        }
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2012-09-23
      • 2019-09-03
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多