2017-01-20 77 views
1

对于学校我必须创建一个iOS应用程序。我想在其他视图中使用UICollectionView。我使用下面的代码,但是当我使用self.libraryCollectionView.reloadData()时,不会调用collectionView函数。名称为libraryCollectionView的CollectionView应该包含图片库。 在这里你可以看到我写的代码。在其他视图中更新集合视图

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

你是否检查过'让pickImage = info [UIImagePickerControllerOriginalImage]为? UIImage'是真的吗?这些代码可能从来没有开始运行。 –

+0

这将返回true,当我调试代码这个images.count它添加一个图像后返回我1。 –

+0

删除numberOfSections方法,如果它没有被使用 – Mamta

回答

1

numberOfSections(in collectionView: UICollectionView)应该返回至少1 它的默认值也为1,所以你可以删除你的整个代码的方法。

+0

这是什么对我有用!非常感谢你,你是英雄! –