2016-11-10 51 views
1

我正在學習如何以編程方式創建UICollectionView。我想在應用程序的另一部分創建從用戶收集的圖片網格。 這個示例代碼能幫助我完成這個嗎?另外,如何配置數據以發出我想要的圖像?我的圖像文件如下所示,UICollectionView的源代碼也是如此。以編程方式創建UICollectionView

UICollectionView:

class PhotosViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 
override func viewDidLoad() { 
    super.viewDidLoad() 
    let imageStore = ImageStore() 
} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
    layout.itemSize = CGSize(width: 100, height: 100) 

    let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    myCollectionView.dataSource = self 
    myCollectionView.delegate = self 
    myCollectionView.registerClass(RDCellCollectionViewCell.self, forCellWithReuseIdentifier: "MyCell") 
    myCollectionView.backgroundColor = UIColor.whiteColor() 
    self.view.addSubview(myCollectionView) 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return images.count 
} 

var images: [UIImage] = [ 

] 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! RDCellCollectionViewCell 
    myCell.imageView.image = images[indexPath.item] 
    myCell.backgroundColor = UIColor.grayColor() 
    return myCell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{ 
    print("User tapped on item \(indexPath.row)") 
} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

ImageStore.Swift:

class ImageStore: NSObject { 

let cache = NSCache() 

func setImage(image: UIImage, forKey key: String) { 
    cache.setObject(image, forKey: key) 

    let imageURL = imageURLForKey(key) 

    if let data = UIImageJPEGRepresentation(image, 0.5) { 
     data.writeToURL(imageURL, atomically: true) 
    } 
} 
func imageForKey(key: String) -> UIImage? { 
    if let existingImage = cache.objectForKey(key) as? UIImage { 
     return existingImage 
    } 

    let imageURL = imageURLForKey(key) 
    guard let imageFromDisk = UIImage(contentsOfFile: imageURL.path!) else { 
     return nil 
    } 

    cache.setObject(imageFromDisk, forKey: key) 
    return imageFromDisk 
} 

func deleteImageForKey(key: String) { 
    cache.removeObjectForKey(key) 

    let imageURL = imageURLForKey(key) 
    do { 
     try NSFileManager.defaultManager().removeItemAtURL(imageURL) 
    } 
    catch let deleteError { 
     print("Error removing the image from disk: \(deleteError)") 
    } 
} 

func imageURLForKey(key: String) -> NSURL { 
    let documentsDirectories = 
    NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
    let documentDirectory = documentsDirectories.first! 

    return documentDirectory.URLByAppendingPathComponent(key) 
} 

}

回答

0

你在正確的軌道上。您需要創建包含UIImageView的UICollectionViewCell的子類;這會讓你在cellForItemAtIndexPath中插入正確的UIImage。

介紹如何掛鉤您的自定義單元格: Create UICollectionViewCell programmatically without nib or storyboard

至於得到正確的圖像,你需要索引路徑某種程度上映射到你的形象店​​,這樣的項目編號對應於正確的圖像鍵。

+0

我回顧了你關聯的問題。我對此很陌生,但仍然很困惑。我應該在我的子類中放什麼?該應用程序的目標是創建用戶在應用程序的另一部分上傳的圖片的UICollectionView。 – Allie

+0

該子類將包含一個UIImageView。您的子類將添加UIImageView作爲其內容視圖的子視圖,並且還需要爲UIImageView提供一個大小。然後,在cellForRowAtIndexPath中,您只需將適當的UIImage插入單元格的UIImageView。 –

+0

本教程看起來不錯:https://randexdev.com/2014/08/uicollectionviewcell/ –

0

如果任務是添加圖像,你應該使用這樣的事情在cellForItemAtIndexPath:

let myCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) 
myCell.backgroundColor = UIColor.blueColor() 
let imageView = UIImageView(frame: cell.contentView.frame) 
cell.contentView.addSubview(imageView) 
imageView.image = //Here you should get right UIImage like ImageStore().imageForKey("YOUR_KEY") 
return myCell 

或者您可以使用自定義UICollectionViewCell子約書亞卡登寫道。

+0

我會在添加圖像之前檢查圖像視圖的存在;否則可能會在圖像視圖頂部添加圖像視圖(因爲這些單元格可能會被重用)。 –