PRE*_*MAR 4 iphone ios uicollectionview swift
在My Project中,我在UICollectionViewCell中创建了Cell
由于未捕获的异常,它有错误终止应用程序
代码如下.
GalleryCell.swift
class GalleryCell: UICollectionViewCell
{
@IBOutlet var titleLabel : UILabel
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
}
Run Code Online (Sandbox Code Playgroud)
我在My ViewController中使用了这个单元格:
代码如下:
NextViewController.swift
import UIKit
class NextViewController: UIViewController
{
@IBOutlet var collectionView : UICollectionView
var ListArray=NSMutableArray()
override func viewDidLoad()
{
super.viewDidLoad()
for i in 0..70
{
ListArray .addObject("C: \(i)")
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section:Int)->Int
{
return ListArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as GalleryCell
cell.titleLabel.text="\(ListArray.objectAtIndex(indexPath.item))"
return cell
}
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
return CGSizeMake(66, 58)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)
}
我的问题是我收到以下错误:
***** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CELL - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'*** First throw call stack:**
Run Code Online (Sandbox Code Playgroud)
PRE*_*MAR 20
我加入以下两行NextViewController.swift下viewDidLoad():
var nibName = UINib(nibName: "GalleryCell", bundle:nil)
collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")
Run Code Online (Sandbox Code Playgroud)
问题是我没有注册笔尖.现在我正在这样做,它工作得很好.
小智 5
与swift中的页眉/页脚相同:
// register header accessory view:
self.collectionView.registerClass(UICollectionReusableView.self,
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
withReuseIdentifier: headerReuseIdentifier);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23158 次 |
| 最近记录: |