I want to know how I can present a new view controller from "didSelectItemAt - Method "
Please note that UIViewController is not a subclass so I don't have access to the navigationController to present the view controller.
class FeedCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.alwaysBounceVertical = true // Making the collection view scrollable vertically
cv.dataSource = self
cv.delegate = self
return cv
}()
override func setupViews(){
super.setupViews()
addSubview(collectionView)
addConstraintWithFormat(format: "H:|[v0]|", views: collectionView)
addConstraintWithFormat(format: "V:|[v0]|", views: collectionView)
collectionView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
You have to implement custom delegate for this. When user select item from collection view, you have to call parent view method using delegate.
Here is reference for this : Access a UICollectionView's parent UIViewController
First give a storyboard id for the viewcontroller then
let destination = UIStoryboard(name: "Storyboard",bundle:nil).instantiateViewController(withIdentifier: "your_viewcontroller_id")
present(destination, animated: false, completion: nil)
Related
I have taken collection view in tableview cell now when I click the collection view cell it will go to the next view controller,is it possible? I have tried did select item method in that how to use perform segue method
I am showing u example
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return false
}
}
This ViewController contain Tableview datasource n delegates. Its cell of type DemoTableViewCell showing below
class DemoTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
var parentVC: UIViewController?
#IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let demoCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DemoCollectionViewCell", for: indexPath) as! DemoCollectionViewCell
return demoCollectionViewCell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
(self.parentVC as! ViewController).performSegue(withIdentifier: "YourSegueName", sender: self)
}
}
Again DemoTableViewCell contain datasouce and delegate of collection view as showing above
Also didSelectItemAt contains performSegue, which is using parent refrence for Segue.
Now in collection view cell, there is 1 variable named parentVC, U have to set this value in cellForRowatIndexpath defined in ViewController class like that
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let demoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "DemoTableViewCell", for: indexPath) as! DemoTableViewCell
demoTableViewCell.parentVC = self
demoTableViewCell.collectionView.reloadData()
return demoTableViewCell
}
and final make a segue from collectionViewCell on storyboard to YourController
I have got a collection view inside a table view (Netflix App Style). I followed this tutorial: https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/
Now I want to implement a function that whenever I click on a collection view, it will pass the employee's name to the next view controller.
Here is my code:
import UIKit
class CategoryRow: UITableViewCell, UICollectionViewDataSource {
#IBOutlet weak var collectionView: UICollectionView!
var jobpost:JobPosition?
var employee: Employee?
var myname = ""
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return jobpost!.Employees.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! EmployeeCell
cell.employeephoto.image = UIImage(named: "spanner")
cell.employeename.text = jobpost?.Employees[indexPath.row].name
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let myname = jobpost?.Employees[indexPath.row].name
//print(myname)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let EmployeeDetailPage = segue.destination as? EmployeeDetailViewController
EmployeeDetailPage?.employeename = myname
print(myname)
}
Now my problem is, the whole PrepareForSegue function didn't actually run. The app runs normally without any error but the data just didn't pass through? (i.e. the print(myname) didn't actually print anything).
Is there something I missed out? Very much appreciated.
Step 1
Create a global variable in your CategoryRow
var myname: String!
Step 2
Change the didSelectItemAt func like below
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
myname = jobpost?.Employees[indexPath.row].name
performSegue(withIdentifier: "EmployeeDetailViewController", sender: myname)
}
Step 3
Create a variable in EmployeeDetailViewController
var employeename: String!
Step 4
Give a identifier to your segue. Here I give "EmployeeDetailViewController"
Then change the prepareForSegue func like below
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EmployeeDetailViewController" {
if let EmployeeDetailVC = segue.destination as? EmployeeDetailViewController {
EmployeeDetailVC.employeename = myname
}
}
}
It looks like your problem is that you are setting a local let constant called myname in your collectionView(_:didSelectItemAt:) method instead of setting your class's myname property.
let myname = jobpost?.Employees[indexPath.row].name
should be:
myname = jobpost?.Employees[indexPath.row].name
I hope someone can help me with this problem. Below is my tvOS app screenshot:
(source: mtiaz.com)
.
I am trying to do two things:
When the user scrolls down on their remote, the second item in the collectionView becomes automatically focused. I need to have it so that the first item is focused instead.
When the user manually shifts focus between items in the collectionView, I want the UI elements in the mid-section of the screen to get updated instead of having to press or select them individually.
Here is my code so far for this screen:
import UIKit
// Global variable
internal let g_CR1 = "channel_cell_01"
class DashboardVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
// outlets
#IBOutlet weak var collectionView: UICollectionView!
#IBOutlet weak var lblChannelName: UILabel!
#IBOutlet weak var imgChannelLogo: UIImageView!
#IBOutlet weak var txtChannelDescription: UITextView!
// variables
var staticData: [Channel] = []
override func viewDidLoad() {
super.viewDidLoad()
// sample channels
initializeSampleData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - CollectionView Callbacks
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return staticData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Get or make a cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: g_CR1, for: indexPath) as! CollectionViewCell
// Configure
cell.imgView.backgroundColor = UIColor.black
cell.imgView.image = staticData[indexPath.row].chLogo
// Return.
return cell
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let currentChannel = staticData[indexPath.row]
DispatchQueue.main.async {
self.lblChannelName.text = currentChannel.chName
self.imgChannelLogo.image = currentChannel.chLogo
self.txtChannelDescription.text = currentChannel.chDescription
}
}
}
I was able to answer point #2. Hopefully someone may benefit from this. However I am still stomped at #1.
func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
// test
print("Previous Focused Path: \(context.previouslyFocusedIndexPath)")
print("Next Focused Path: \(context.nextFocusedIndexPath)")
let currentChannel = staticData[(context.nextFocusedIndexPath?[1])!]
DispatchQueue.main.async {
self.lblChannelName.text = currentChannel.chName
self.imgChannelLogo.image = currentChannel.chLogo
self.txtChannelDescription.text = currentChannel.chDescription
}
}
I have a collection view that should shows camera roll photos But It didn't show any thing!
I saw similar questions But Their code didn't work !
I added privacy for using album photos in Info.plist
here is my code
class ViewController: UICollectionViewController , UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
grabPhotos()
}
func grabPhotos() {
var imageArray = [UIImage]()
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
if let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions) {
if fetchResult.count > 0 {
for i in 0..<fetchResult.count {
imgManager.requestImage(for: fetchResult.object(at: i) , targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
image , error in
imageArray.append(image!)
})
}
}
else {
print("No Photos")
self.collectionView?.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
let width = collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
}
It Shows Nothing I read Similar questions But they didn't help me
i am working on swift 3.0.1 and xcode 8.2.1,
i want to select multiple images from gallery and show it in different collage in collectionView, when i select images from gallery and press done button collection view did not show anything but when i press Mobile Screen then images shown on the screen. tell me whats wrong in my code and how to get my task. I import these frameworks Photos, BSImagePicker, BSGridCollectionViewLayout. here is my code
#IBOutlet weak var collection: UICollectionView!
var imagePicker = UIImagePickerController()
var imageArray = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imagePicker.delegate = self
collection.delegate = self
collection.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func Camera(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera){
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
imagePicker.cameraCaptureMode = .photo
imagePicker.cameraFlashMode = .auto
imagePicker.modalPresentationStyle = .fullScreen
self.present(imagePicker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "No Camera", message: "Your Device has No Camera", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .destructive, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
#IBAction func Library(_ sender: Any) {
let vc = BSImagePickerViewController()
vc.maxNumberOfSelections = 4
//vc.takePhotoIcon = UIImage(named: "Tick")
vc.albumButton.tintColor = UIColor.blue
vc.cancelButton.tintColor = UIColor.blue
vc.doneButton.tintColor = UIColor.blue
vc.selectionCharacter = "✓"
vc.selectionFillColor = UIColor.green
vc.selectionStrokeColor = UIColor.clear
vc.selectionShadowColor = UIColor.black
vc.selectionTextAttributes[NSForegroundColorAttributeName] = UIColor.lightGray
vc.cellsPerRow = {( verticalSize: UIUserInterfaceSizeClass, horizontalSize: UIUserInterfaceSizeClass) -> Int in
switch (verticalSize, horizontalSize){
case(.compact , .regular):
return 3
case (.compact, .compact): // iPhone5-6 landscape
return 3
case (.regular, .regular): // iPad portrait/landscape
return 3
default:
return 3
}
}
bs_presentImagePickerController(vc, animated: true, select: { (asset: PHAsset) -> Void in
print("Selected: \(asset)")
let imageManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
imageManager.requestImage(for: asset, targetSize: CGSize(width: 132, height: 114) ,contentMode: .aspectFill, options: requestOptions, resultHandler:
{
image, error in
self.imageArray.append(image!)
})
self.collection.reloadData()
},
deselect: { (asset: PHAsset) -> Void in
print("Deselected: \(asset)")
},
cancel: { (asset: [PHAsset]) -> Void in
print("Cancel: \(asset)")
},
finish: { (asset: [PHAsset]) -> Void in
print("Finish: \(asset)")
}, completion: nil)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let Cell : CellClass = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CellClass
Cell.imageView.image = self.imageArray[indexPath.row]
return Cell
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageArray.append(pickedImage)
}
else {
print("Image not Picked")
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.imagePicker = UIImagePickerController()
dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width / 3 - 1
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.red
}
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.green
}