我试图以编程方式在Swift中创建一个UICollectionView。我可以设置它,但是一旦我尝试在自己身上使用更复杂的操作,例如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)
}
}
实现deleteItemsAtIndexPath
和insertItemsAtIndexPath
的关键是需要确保collectionView:numberOfItemsInSection:
在删除或插入后返回正确数量的项。例如,如果表当前有10个元素,并且调用deleteItemsAtIndexPath
,则需要确保下一次集合视图请求元素数量时返回9,否则将崩溃。集合视图不会为您增加或减少该数字。
您的问题可能是没有展开可选值。
例如,尝试这样做
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]
修复打开可选的。
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
self.collectionView.insertItemsAtIndexPaths([newIndexPath!])
...etc
}