我正在努力学习如何使用UICollectionView
.该文件是一个有点难以理解,而我发现教程无论是在目标C或长期复杂的项目当中.
当我学习如何使用时UITableView
,我们❤Swift的 如何用iOS 8和Swift制作一个简单的桌面视图有一个非常基本的设置和解释让我去.有这样的东西UICollectionView
吗?
下面的答案是我试图学习如何做到这一点.
我UICollectionView
在Swift 中使用了一个,但是当我尝试更改单元格标签的文本时,我得到了.
func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
return 5
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("title", forIndexPath: indexPath) as TitleCollectionViewCell
// Next line: fatal error: unexpectedly found nil while unwrapping an Optional value
cell.labelTitle.text = "This is a title"
return cell
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这个?
我有一个collectionViewController,我想显示一堆自定义的UICollectionViewCells,上面有一些标签.不幸的是,每当我尝试访问自定义UICollectionViewCell的标签时,它都会导致崩溃:
安慰
致命错误:无法打开Optional.None
窗口
Thread1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0x0)
我正试图像这样访问标签:
cell.name.text = names[indexPath!.item]
Run Code Online (Sandbox Code Playgroud)
也许这来自我的插座标签是零?但是,寻找答案没有任何效果,因为我不确定添加的问题是什么?/!在我的代码中并没有真正帮助.
MyCustomUICollectionViewController
class ScrambledTextCollectionViewController: UICollectionViewController {
var names: String[] = ["Anna", "Alex", "Brian", "Jack"]
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView.registerClass(MyCustomCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
return names.count
}
override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
var cell = collectionView?.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as MyCustomCollectionViewCell
cell.name.text …
Run Code Online (Sandbox Code Playgroud) 编辑:似乎与此相同的问题
我使用自定义布局和单元格制作了集合视图,但我的单元格的位置有点偏离,所以我决定给它们编号,以便我可以更轻松地调试它。我按照这个人的回答使用标签为我的单元格编号。不知何故myLabel
为零并导致我的应用程序崩溃。
我相信我已经正确连接了插座,但也许有人可以提供一个列表让我检查我是否做错了什么。
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var gridView: UICollectionView!
private let reuseIdentifier = "DesignCell"
private let numberOfSections = 1
private let numberOfCircles = 48
override func viewDidLoad() {
super.viewDidLoad()
self.gridView.registerClass(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return numberOfSections
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfCircles
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = …
Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
我正在使用 Alamofire 从网络服务获取图像。我使用以下代码在 UICollectionView 的自定义单元格中显示这些图像
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let reuseIdentifier = "cell"
// get a reference to our storyboard cell
let cell : ImageCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellCollection", forIndexPath: indexPath) as! ImageCollectionCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.backgroundColor = UIColor.yellowColor() // make cell more visible in our example project
let imageGalleryURLString = self.MutableArray.objectAtIndex(indexPath.row) .valueForKey("thumbnail") as! String
let imageURL = NSURL(string:imageGalleryURLString)
cell.imageviewCollectionCell.sd_setImageWithURL(imageURL)
return cell …
Run Code Online (Sandbox Code Playgroud)