Selecting a time Interval(collection View) - swift

enter image description here
In my project i have to select the time interval(range) as the below image.
Here is the code i am using, i have added a collectionView :
class ViewController: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource{
#IBOutlet var collectionView: UICollectionView!
var time = ["0.00","1.00","2.00","3.00","4.00","5.00","6.00","7.00","8.00","9.00","10.00","11.00","12.00","1.00","2.00","3.00","4.00","5.00","6.00","7.00","8.00","9.00","10.00","11.00"]
var timeVal = ["AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","AM","PM","PM","PM","PM","PM","PM","PM","PM","PM","PM","PM","PM"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return time.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! timeCell
cell.timeLabel.text = time[indexPath.row]
cell.intervalLabel.text = timeVal[indexPath.row]
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake((collectionView.frame.size.width/7) , (collectionView.frame.size.height))
}
}
And i appears as the below image:
enter image description here
But i am confused how to implement that interval selection(Two time intervals).Please suggest.

Related

Collection view inside a collection view

As of title, I'm having trouble with the issue of nesting collectionViews. I found a workaround gimmicky solution but I'm not really satisfied with the result:
class ViewController3: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var outCollection: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
outCollection.dataSource = self
outCollection.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath:
IndexPath) -> UICollectionViewCell {
let cell = outCollection.dequeueReusableCell(withReuseIdentifier: "outCell", for:
indexPath) as! CollectionViewCell
cell.backgroundColor = .green
cell.setViewDD() // gimmick HERE
return cell
}
}
and for the CollectionViewCell
class CollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var inCollection: UICollectionView!
func setViewDD() {
self.inCollection.delegate = self
self.inCollection.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = inCollection.dequeueReusableCell(withReuseIdentifier: "inCell", for: indexPath) as! innercellulosa
cell.backgroundColor = .red
return cell
}
}
if I try to set the delegate and datasource of the innerCollection (either in VC3 or CVCell) I stumble in multiple problems which I can't solve.

Cant display image in UICollectionView cell

I need to display images in collection view cells but when I'm trying to do that I'm getting 10 empty cells and I don't know where im making mistakes
Here is my code of ViewController
class NewGalleryViewController: UIViewController {
var presenter: ViewToPresenterPhotoProtocol?
var builder: GalleryRequestBuilder?
#IBOutlet var collectionView: UICollectionView!
let reuseIdentifier = "customCVCell"
#objc func refresh() {
presenter?.refresh()
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupPresenterIfNeed()
presenter?.viewDidLoad()
// Do any additional setup after loading the view.
}
func setupPresenterIfNeed() {
self.collectionView.backgroundColor = UIColor.white
if self.presenter == nil {
let presenter = GalleryPresenter()
presenter.view = self
self.presenter = presenter
self.builder = GalleryRequestBuilder()
}
}
}
extension NewGalleryViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.presenter?.photos.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCollectionViewCell
KFImage.url(builder?.createImageUrl(name: (presenter?.photos[indexPath.item].name)!))
.onSuccess { result in
cell.imageView.image = result.image
}
return cell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 180, height: 128)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 20.0
}
// MARK: - UICollectionViewDelegate protocol
private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
}
extension NewGalleryViewController: PresenterToViewPhotoProtocol{
func onFetchPhotoSuccess() {
self.collectionView.reloadData()
self.collectionView!.collectionViewLayout.invalidateLayout()
self.collectionView!.layoutSubviews()
self.collectionView.refreshControl?.endRefreshing()
}
func onFetchPhotoFailure(error: String) {
print("View receives the response from Presenter with error: \(error)")
self.collectionView.refreshControl?.endRefreshing()
}
}
And Here is the code of cell
class PhotoCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
I've checked the link I'm making request to and it works. So problem is not in link. Maybe I should reload items after getting images?
You should set your UICollectionView delegate and data source once the view is loaded:
override func viewDidLoad() {
super.viewDidLoad()
// Add this lines
collectionView.delegate = self
collectionView.dataSource = self
self.setupPresenterIfNeed()
presenter?.viewDidLoad()
}

UICollectionView first reusable table cell not showing up

I am working on a game right now and my level selection isn't working right.
The first cell in the collection view won't show up and I can't figure out why.
Here is what it looks like:
Picture of collection view with missing cell
And here is my code for the UICollectionViewController:
the reuse IdentifierList is a list of strings that corresponds to the cells in the storyboard version of this. and I have already checked that they are all spelled correctly.
The one missing is the "MyCell" cell.
class UICollectionCollectionViewController: UICollectionViewController {
var reuseIdentifierList = ["MyCell",
"MyCell2",
"MyCell3",
"MyCell4",
"MyCell5",
"MyCell6",
"MyCell7",
"MyCell8",
"MyCell9",
"MyCell10",
"MyCell11",
"MyCell12",
"MyCell13",
"MyCell14",
"MyCell15",
"MyCell16",
"MyCell17",
"MyCell18"
]
override func viewDidLoad() {
super.viewDidLoad()
reuseIdentifier = reuseIdentifierList[0]
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return reuseIdentifierList.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[indexPath.item], for: indexPath)
if indexPath.item != indexPath.startIndex {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[indexPath.item], for: indexPath)
}else{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierList[0], for: indexPath)
}
print(indexPath.item)
print(reuseIdentifierList[indexPath.item])
// Configure the cell
return cell
}
#IBAction func unwindToViewControllerLevel (sender: UIStoryboardSegue){
}
}
Thanks in advance
There is no point in creating that many cells. Just have an array to serve as your data source. In your case, it looks like you want your cells to have a button with numbers in order.
class UICollectionCollectionViewController: UICollectionViewController {
// let dataSourceArray = [Int](1...20) // [1, 2, 3 ..., 20]
// EDIT -- SegueId
let dataSourceArray = [(name: "1", segueId: "segueId1"), (name: "2", segueId: "segueId2"), (name: "3", segueId: "segueId3")]
let cellId = "customCellId"
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: cellId)
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return dataSourceArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
let item = dataSourceArray[indexPath.item]
cell.button.setTitle(item.name, for: .normal)
let segueId = item.segueId
// use segue id to perform different segues
return cell
}

Why won't my Collection View Cells display in the iPhone Simulator?

I am trying to programmatically create a Collection View with six cells. The iPhone Simulator in Xcode 8 told me that my build was successful. There is still an error though.
Any help spotting the bug or bugs that are causing my cells not to display in the Simulator is greatly appreciated. (I put the Collection View directly into the IB from the Object Library. Also, I want each cell to occupy the entire size of the Collection View, hence displayedCellDimensions, because the user will segue between cells when a forward or backward button is tapped.) Thank you!
import UIKit
class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
//MARK: - Collection View Properties
#IBOutlet weak var ibCollectionView: UICollectionView!
var cellArray:[customCollectionViewCell] = Array(repeating: customCollectionViewCell(), count: 6)
let displayedCellDimensions = CGSize(width: 343, height: 248)
//MARK: - Xcode-generated Methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
ibCollectionView.dataSource = self
ibCollectionView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - Collection View Methods
//Place cells in collection view and change the cell size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if collectionView.visibleCells.isEmpty {
for cell in 0...cellArray.count {
collectionView.addSubview(cellArray[cell])
return CGSize(width: 343, height: 248)
}
}
return CGSize(width: 343, height: 248)
}
//Register a new cell
func registerCell(_ collectionView: UICollectionView) {
for _ in 0...cellArray.count {
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellArray.count
}
...}
In your viewDidLoad() Try:
viewDidLoad() {
ibCollectionView.dataSource = self
ibCollectionView.delegate = self
}
Add this to the top of your class:
class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
You must use these functions in your ViewController class:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}

App crashes with fatal error for setting image to CustomViewCell

I have created an UICollectionView with custom UICollectioViewCell with just an image view. When I try add an image to the image view in the cell it crashes with
fatal error: unexpectedly found nil while unwrapping an Optional value.
I'm unable to figure out where i'm getting the nil value from.
Below is my code for the CollectionViewController.
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let reuseIdentifier = "Cell"
#IBOutlet weak var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(PhotoViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoViewCell
// Configure the cell
cell.backgroundColor = UIColor.grayColor()
cell.cellImageView.image = UIImage(named:"IMG_1635")// The code crashes here....
return cell
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
}
// Code for Custom Cell
class PhotoViewCell: UICollectionViewCell {
#IBOutlet weak var cellImageView: UIImageView!
}
The app runs fine when on removing the following line cell.cellImageView.image = UIImage(named:"IMG_1635")
Try:
cell.cellImageView.image = UIImage(named:"IMG_1635")