Collection View 笔记
01 在故事板中拖一个Collection View控件,设置个图片,加一个imageView,设置它的大小,使其大小适应,
将ViewController的dataSource连接上,要是不连接的,在viewController中写 collentionView.Datasourece = self
02 新建并指定ConllectionViewCell 对应的的类(继承UIviewControllerCell),设置它的Identifier
03 在ViewController中,让它继承UICollectionViewDataSource
04 然后实现UICollectionViewDataSource协议的方法
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1//返回的行数
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! myCellCollectionViewCell // myCell是ConllectionViewCell的Identifier,as后面是ConllectionViewCell对应的类
cell.imageView.image = UIImage(named: "moon-1859616_1920.jpg”)//图片的名字
return cell //返回cell 的内容
}