我试图以编程方式在Swift中创建一个UICollectionView。 我可以设置它,但是一旦我尝试使用更复杂的操作,例如insertItemsAtIndexPath或者我的self.collectionView上的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)
}
}
答案 0 :(得分:0)
实施deleteItemsAtIndexPath
和insertItemsAtIndexPath
的关键是,您需要确保collectionView:numberOfItemsInSection:
在删除或插入后返回正确数量的项目。例如,如果表当前有10个元素并且您调用deleteItemsAtIndexPath
,则需要确保下次集合视图询问元素数量时,它将返回9或者它将崩溃。集合视图不会处理为您增加或减少该数字。
答案 1 :(得分:0)
这里的问题可能不是展开可选值。
所以例如尝试这样做
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
}