I am completely new to iOS development. I have never written code in my life. But I'm trying to follow this guide here . I have successfully completed the tutorial, but I want to change the code a bit.
- How can I add the text “Title” under each uploaded image?
- And also, how can I change the color of the border of the image and the background of the application?
ViewController
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
var galleryItems: [GalleryItem] = []
var listOfDescriptions = [String]()
override func viewDidLoad() {
super.viewDidLoad()
initGalleryItems()
collectionView.reloadData()
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
private func initGalleryItems() {
var items = [GalleryItem]()
let inputFile = NSBundle.mainBundle().pathForResource("items", ofType: "plist")
let inputDataArray = NSArray(contentsOfFile: inputFile!)
for inputItem in inputDataArray as! [Dictionary<String, String>] {
let galleryItem = GalleryItem(dataDictionary: inputItem)
items.append(galleryItem)
}
galleryItems = items
}
private func populateList() {
listOfDescriptions.append("Valhaha is a flavor");
listOfDescriptions.append("Unicorns blood is made from dead unicorns. Harry Potter in this bit");
}
private func getDescription(position: Int) -> String {
return listOfDescriptions[position]
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return galleryItems.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GalleryItemCollectionViewCell", forIndexPath: indexPath) as! GalleryItemCollectionViewCell
cell.setGalleryItem(galleryItems[indexPath.row])
return cell
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let commentView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "GalleryItemCommentView", forIndexPath: indexPath) as! GalleryItemCommentView
commentView.commentLabel.text = "Supplementary view of kind \(kind)"
return commentView
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let alert = UIAlertController(title: "didSelectItemAtIndexPath:", message: "Indexpath = \(indexPath)", preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "Dismiss", style: .Destructive, handler: nil)
alert.addAction(alertAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let picDimension = self.view.frame.size.width / 4.0
return CGSizeMake(picDimension, picDimension)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let leftRightInset = self.view.frame.size.width / 14.0
return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset)
}
}
GalleryItemCollectionCellView
import UIKit
class GalleryItemCollectionViewCell: UICollectionViewCell {
@IBOutlet var itemImageView: UIImageView!
func setGalleryItem(item:GalleryItem) {
itemImageView.image = UIImage(named: item.itemImage)
}
}
source
share