运行 App 时 UICollectionView 显示为黑色
UICollectionView appears black when running the App
我正在使用 swift 2.0 和 xcode 7。我想在我的应用程序上有一个带有图像数组的垂直集合视图。但是,当我 运行 我的项目时,UICollectionView 区域显示为黑色。我已经在我的 ViewController 上设置了脚本,但我不知道我缺少什么。我已经设置了一个标识符和所有其他东西。有人可以帮帮我吗?谢谢
我猜你忘记了委托,这是典型的错误。
另外,尝试使用此代码示例
class ExampleCollectionView: UIViewController, UICollectionViewDelegate
{
@IBOutlet weak var collectionView: UICollectionView!
let tshirtsArray = ["tshirt_1.png",
"tshirt_2.png",
"tshirt_3.png",
"tshirt_4.png"]
override func viewDidLoad()
{
super.viewDidLoad()
collectionView.delegate = self
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.tshirtsArray.count
}
func collectionView(cv: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: UICollectionViewCell = cv.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
let collectionImageView: UIImageView = cell.viewWithTag(100) as! UIImageView
collectionImageView.image = UIImage(named: self. tshirtsArray[indexPath.row])
return cell
}
}
我已经让 UICollectionView 显示图像,但我仍然需要更新尺寸。我已经按照@pdobsk 的建议查看错误消息,它说我应该考虑单元格大小以及边距和其他内容,因为单元格大小与集合视图的大小相同。
我正在使用 swift 2.0 和 xcode 7。我想在我的应用程序上有一个带有图像数组的垂直集合视图。但是,当我 运行 我的项目时,UICollectionView 区域显示为黑色。我已经在我的 ViewController 上设置了脚本,但我不知道我缺少什么。我已经设置了一个标识符和所有其他东西。有人可以帮帮我吗?谢谢
我猜你忘记了委托,这是典型的错误。 另外,尝试使用此代码示例
class ExampleCollectionView: UIViewController, UICollectionViewDelegate
{
@IBOutlet weak var collectionView: UICollectionView!
let tshirtsArray = ["tshirt_1.png",
"tshirt_2.png",
"tshirt_3.png",
"tshirt_4.png"]
override func viewDidLoad()
{
super.viewDidLoad()
collectionView.delegate = self
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.tshirtsArray.count
}
func collectionView(cv: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: UICollectionViewCell = cv.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
let collectionImageView: UIImageView = cell.viewWithTag(100) as! UIImageView
collectionImageView.image = UIImage(named: self. tshirtsArray[indexPath.row])
return cell
}
}
我已经让 UICollectionView 显示图像,但我仍然需要更新尺寸。我已经按照@pdobsk 的建议查看错误消息,它说我应该考虑单元格大小以及边距和其他内容,因为单元格大小与集合视图的大小相同。