对于学校我必须制作iOS应用程序。我想在其他视图中使用UICollectionView。我使用以下代码,但是当我使用self.libraryCollectionView.reloadData()时,不会调用collectionView函数。名为libraryCollectionView的CollectionView应该包含一个Images库。 在这里你可以看到我写的代码。
import UIKit
class CoverViewController: UIViewController, UICollectionViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDataSource {
// MARK: Properties
@IBOutlet weak var photoImageView: UIImageView!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var libraryCollectionView: UICollectionView!
var cover: Cover?
var images = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
// Set up views if editing an existing Cover.
if let cover = cover {
photoImageView.image = cover.image
typeLabel.text = cover.type
}
libraryCollectionView.delegate = self
libraryCollectionView.dataSource = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
@IBAction func addImage(_ sender: UIBarButtonItem) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet);
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
self.present(imagePicker, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicker.sourceType = .photoLibrary
self.present(imagePicker, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
// Methods for ImagePicker
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
images.append(pickedImage)
DispatchQueue.main.async{
self.libraryCollectionView.reloadData()
}
}
dismiss(animated: true, completion: nil)
}
// Methods for CollectionView
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellIdentifier = "cell"
let cell = libraryCollectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ImageCellView
NSLog(String(images.count))
cell.photoImageView.image = images[indexPath.row]
return cell
}
}
答案 0 :(得分:1)
numberOfSections(in collectionView: UICollectionView)
应至少返回1。
它的默认值也是1,因此您只需从代码中删除整个方法即可。