Ios 如何使用Swift创建简单的集合视图
我正在尝试学习如何使用Ios 如何使用Swift创建简单的集合视图,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试学习如何使用UICollectionView。这有点难理解,我发现的教程要么是在Objective C中,要么是很长很复杂的项目 当我学习如何使用UITableView时,我们❤ 斯威夫特有一个非常基本的设置和解释让我去。对于UICollectionView,是否有类似的内容 下面的答案是我尝试学习如何做到这一点。这个项目已经用Xcode 10和Swift 4.2进行了测试 创建新项目 它可以只是一个单一的视图应用程序 添加代码 创建一个新的Cocoa Touch类文件(文件>新建>文
UICollectionView
。这有点难理解,我发现的教程要么是在Objective C中,要么是很长很复杂的项目
当我学习如何使用UITableView
时,我们❤ 斯威夫特有一个非常基本的设置和解释让我去。对于UICollectionView
,是否有类似的内容
下面的答案是我尝试学习如何做到这一点。这个项目已经用Xcode 10和Swift 4.2进行了测试
创建新项目
它可以只是一个单一的视图应用程序
添加代码
创建一个新的Cocoa Touch类文件(文件>新建>文件…>iOS>Cocoa Touch类)。将其命名为MyCollectionViewCell。此类将保存添加到情节提要单元中的视图的输出
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
}
稍后我们将连接此插座
打开ViewController.swift并确保您具有以下内容:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]
// MARK: - UICollectionViewDataSource protocol
// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.row] // The row value is the same as the index of the desired text within the array.
cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
return cell
}
// MARK: - UICollectionViewDelegate protocol
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
}
注释
和UICollectionViewDataSource
是集合视图遵循的协议。您还可以添加UICollectionViewDelegate
协议,以编程方式更改视图的大小,但这不是必需的UICollectionViewFlowLayout
- 我们只是在网格中放置简单的字符串,但您以后肯定可以制作图像
- 项目:1
- 布局:流程
let reuseIdentifier=“cell”
值相同
在单元格的标识检查器中,将类名设置为我们创建的自定义类MyCollectionViewCell
连接插座
- 将收集单元格中的标签挂接到
类中的MyCollectionViewCell
。(你可以。)myLabel
- 将集合视图
和委托
挂接到视图控制器。(在文档大纲中的集合视图上单击鼠标右键。然后单击并向上拖动加号箭头到视图控制器。)数据源
层
。下面是一些示例代码。您可以将它直接放在上面代码中的cell.backgroundColor=UIColor.cyan
后面
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8
请参阅,以了解可以对该层执行的其他操作(例如阴影)
点击时更改颜色
当电池对点击做出视觉反应时,它有助于更好的用户体验。实现这一点的一种方法是在触摸单元格时更改背景颜色。为此,请将以下两种方法添加到ViewController
类中:
// change background color when user touches cell
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.red
}
// change background color back when user releases touch
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.cyan
}
以下是更新后的外观:
进一步研究
//MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1 //return number of sections in collection view
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10 //return number of rows in section
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath)
configureCell(cell, forItemAtIndexPath: indexPath)
return cell //return your cell
}
func configureCell(cell: UICollectionViewCell, forItemAtIndexPath: NSIndexPath) {
cell.backgroundColor = UIColor.blackColor()
//Customise your cell
}
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionCell", forIndexPath: indexPath) as UICollectionReusableView
return view
}
//MARK: UICollectionViewDelegate
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// When user selects the cell
}
override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
// When user deselects the cell
}
UICollectionView与UITableView相同,但它为我们提供了创建网格视图的附加功能,这在UITableView中有点问题。这将是一篇很长的文章,我提到了一个简单的步骤,您可以从中获得一切。UICollectionView的实现非常有趣。 您可以使用简单的源代码并使用以下链接观看视频教程:
对于
swift 4.2
--
我的手机号码是零,它正在崩溃。知道为什么吗?Im使用自定义布局如果您没有通过将控件从情节提要中的标签拖动到代码中的
myLabel
的@IBOutlet
来连接插座,您将遇到类似的崩溃。如果您使用interface builder并使用插座引用单元,请不要在控制器中注册自定义单元类。如果UICollectionViewCell outlet为零,则必须删除self.collectionView.registerClass(MyCollectionViewCell.self,forCellWithReuseIdentifier:“Cell”)
。如果您仍然有问题,请检查reuseIdentifier
在dequeueReusableCellWithReuseIdentifier
和Storyboard中的reuseidentier
是否相同。我希望苹果的文档能够像这个解释一样容易阅读
extension ViewController : UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return nameArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DummyCollectionCell", for: indexPath) as! DummyCollectionCell
cell.titleLabel.text = nameArr[indexPath.row]
cell.userImageView.backgroundColor = .blue
return cell
}
}
extension ViewController : UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let alert = UIAlertController(title: "Hi", message: "\(nameArr[indexPath.row])", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
//MARK: UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1 //return number of sections in collection view
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10 //return number of rows in section
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath)
configureCell(cell: cell, forItemAtIndexPath: indexPath)
return cell //return your cell
}
func configureCell(cell: UICollectionViewCell, forItemAtIndexPath: NSIndexPath) {
cell.backgroundColor = UIColor.black
//Customise your cell
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collectionCell", for: indexPath as IndexPath) as UICollectionReusableView
return view
}
//MARK: UICollectionViewDelegate
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// When user selects the cell
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
// When user deselects the cell
}