fetch name from collection view cell while clicking on checkbox - swift3

I have name and image in collection view cell. I want to fetch names which I've selected on clicking checkbox in that particular cell. How do I do this??
Below is my code in collection view
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
cell.imgView.sd_setImage(with: URL(string: str1+self.nameArr [indexPath.row]), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblTime.text = self.timeArr [indexPath.row]
return cell
}
Here's my cell file:
class MyCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var checkButton: UIButton!
#IBOutlet var imgView: UIImageView!
#IBOutlet weak var lblName: UILabel!
#IBOutlet weak var lblTime: UILabel!
var tickCheck:Bool = false
#IBAction func btnCheck(_ sender: Any)
{
if tickCheck == false
{
checkButton.setBackgroundImage(UIImage(named: "check"), for: .normal)
tickCheck = true
}
else
{
checkButton.setBackgroundImage(UIImage(named: "uncheck"), for: .normal)
tickCheck = false
}
}
}

Your CollectionView
class collectionVC: UIViewController,CellDelegate {
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! MyCollectionViewCell
cell.imgView.sd_setImage(with: URL(string: str1+self.nameArr [indexPath.row]), placeholderImage: UIImage(named: ""))
cell.lblName.text = self.nameArr [indexPath.row]
cell.lblTime.text = self.timeArr [indexPath.row]
cell.checkButton.tag = indexPath.item
cell.delegateCell = self
cell.indexPath = indexPath
return cell
}
//Mark:- Did tap cell
func didTapCell(index: IndexPath){
let name = self.nameArr[index.item]
print("Your name >> \(name)")
}
}
Collection Cell
protocol CellDelegate: class {
func didTapCell(index: IndexPath)
}
class MyCollectionViewCell: UICollectionViewCell {
#IBOutlet weak var checkButton: UIButton!
#IBOutlet var imgView: UIImageView!
#IBOutlet weak var lblName: UILabel!
#IBOutlet weak var lblTime: UILabel!
var delegateCell:CellDelegate?
var indexPath:IndexPath?
var tickCheck:Bool = false
#IBAction func btnCheck(_ sender: Any)
{
delegateCell?.didTapCell(index: indexPath!)
if tickCheck == false
{
checkButton.setBackgroundImage(UIImage(named: "check"), for: .normal)
tickCheck = true
}
else
{
checkButton.setBackgroundImage(UIImage(named: "uncheck"), for: .normal)
tickCheck = false
}
}
}

Related

How to navigate to different storyboards along with sidemenu in swift 3

I am using third party control for sidemenu named : MMDrawerController, and m handling UI using multiple storyboards.let me come to the point my sidemenu looks like this : Sidemenu Image
Trying to achieve :
1)When I click on the Parent, "main.storyboard" should be displayed.
2)When I click on the Management, "management.storyboard" should be displayed.
same sidemenu should be displayed across all storyboard file.
I have tried some code by my own but m not getting the sidemenu on "management.storyboard" :(
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch(indexPath.row)
{
case 4:
let mainstoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var welview = mainstoryboard.instantiateViewController(withIdentifier: "WelcomeParentViewController") as! WelcomeParentViewController
var welnav = UINavigationController(rootViewController: welview)
var appdel : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appdel.centerContainer!.centerViewController = welnav
appdel.centerContainer!.toggle(MMDrawerSide.left, animated: true, completion: nil)
break
case 5:
let mainstoryboard : UIStoryboard = UIStoryboard(name: "Management-Storyboard", bundle: nil)
var welview = mainstoryboard.instantiateViewController(withIdentifier: "ReportsViewController") as! ReportsViewController
var welnav = UINavigationController(rootViewController: welview)
var appdel : AppDelegate = UIApplication.shared.delegate as! AppDelegate
appdel.centerContainer!.centerViewController = welnav
appdel.centerContainer!.toggle(MMDrawerSide.left, animated: true, completion: nil)
break
default :
break
}
I want same sidemenu across all storyboard file.
how to accomplish the above feature.Please Help.Thank you in advance.
MMDrawerController Code inside the appdelegate.swift
import UIKit
import GoogleMaps
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var centerContainer : MMDrawerController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if isUserLoggedIn()
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.gotoMainvc()
}
else
{
let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let setViewController = mainStoryboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)
}
return true
}
func isUserLoggedIn() -> Bool{
if let accessToken = UserDefaults.standard.object(forKey: "access_token") as? String
{
if (accessToken.characters.count) > 0{
return true
} else {
return false
}
}
else {
return false
}
}
func gotoMainvc()
{
var rootviewcontroller = self.window?.rootViewController
if(UIDevice.current.userInterfaceIdiom == .phone)
{
let mainstoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var centerviewcontroller = mainstoryboard.instantiateViewController(withIdentifier: "WelcomeParentViewController") as! WelcomeParentViewController
var leftsideviewcontroller = mainstoryboard.instantiateViewController(withIdentifier: "LeftSideMenuViewController") as! LeftSideMenuViewController
let leftsidenav = UINavigationController(rootViewController: leftsideviewcontroller)
let centernav = UINavigationController(rootViewController: centerviewcontroller)
centerContainer = MMDrawerController(center: centernav, leftDrawerViewController: leftsidenav)
centerContainer?.openDrawerGestureModeMask = MMOpenDrawerGestureMode.panningCenterView
centerContainer?.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.panningCenterView
window?.rootViewController = centerContainer
window?.makeKeyAndVisible()
}
else{
let mainstoryboard2 : UIStoryboard = UIStoryboard(name: "Storyboard-iPad", bundle: nil)
var centerviewcontroller = mainstoryboard2.instantiateViewController(withIdentifier: "WelcomeParentViewController") as! WelcomeParentViewController
var leftsideviewcontroller = mainstoryboard2.instantiateViewController(withIdentifier: "LeftSideMenuViewController") as! LeftSideMenuViewController
let leftsidenav = UINavigationController(rootViewController: leftsideviewcontroller)
let centernav = UINavigationController(rootViewController: centerviewcontroller)
centerContainer = MMDrawerController(center: centernav, leftDrawerViewController: leftsidenav)
centerContainer?.openDrawerGestureModeMask = MMOpenDrawerGestureMode.panningCenterView
centerContainer?.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.panningCenterView
window?.rootViewController = centerContainer
window?.makeKeyAndVisible()
}
}
//MARK: sharedDelegate
func sharedDelegate() -> AppDelegate
{
return UIApplication.shared.delegate as! AppDelegate
}
}
Main View Controller
import UIKit
class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource , UIGestureRecognizerDelegate {
#IBOutlet weak var collectioncell: UICollectionView!
var objectProfile:SideMenuViewController!
var tapGesture = UITapGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myviewTapped(_:)))
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1
collectioncell.addGestureRecognizer(tapGesture)
collectioncell.isUserInteractionEnabled = true
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.objectProfile = storyboard.instantiateViewController(withIdentifier: "SideMenuViewController") as! SideMenuViewController
self.objectProfile.view.frame = CGRect(x: -(self.view.frame.size.width - 40), y: 0, width: self.view.frame.size.width - 40, height: self.view.frame.size.height)
self.view.addSubview(self.objectProfile.view)
self.navigationController?.didMove(toParentViewController: self.objectProfile)
self.collectioncell.layer.cornerRadius = 5.0
self.collectioncell.layer.borderWidth = 5.0
self.collectioncell.clipsToBounds = true
self.collectioncell.layer.borderColor = UIColor.clear.cgColor
self.collectioncell.layer.masksToBounds = true
}
func myviewTapped(_ sender: UITapGestureRecognizer) {
if self.objectProfile.view.isHidden == false {
UIView.animate(withDuration: 0.3)
{
self.objectProfile.view.frame = CGRect(x: -(self.view.frame.size.width - 90), y: 0, width: self.view.frame.size.width - 90, height: self.view.frame.size.height)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func Mrnupressed(_ sender: UIBarButtonItem) {
UIView.animate(withDuration: 0.3)
{
self.objectProfile.view.frame = CGRect(x: 0 , y: 0, width: (self.view.frame.size.width - 100), height: self.view.frame.size.height)
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! MainCollectionViewCell
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let cellsize = CGSize(width: (collectioncell.bounds.size.width/2) - 12, height:(collectioncell.bounds.size.height/3) - 20)
return cellsize
}
}
Child View Controller
import UIKit
class SideMenuViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var sidemenutable: UITableView!
var stri:String!
var ArrarMenu:[String] = ["Home","SiteMep","Student","About Us","Help"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ArrarMenu.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SideMenuCell") as! SideMenuTableViewCell
let objarray = ArrarMenu[indexPath.row]
cell.lblitem.text = objarray
stri = objarray
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0{
self.performSegue(withIdentifier: "SegueForHome", sender: self)
}
}
}
In This Code I Am use Child View As Side Manu Controller
here is code open side from other storyboard
#IBAction func menutapped(_ sender: Any) {
var appdelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.centerContainer?.toggle(MMDrawerSide.left, animated: true, completion: nil)
}

Swift 3.1 Label in TableViewCell - unexpectedly found nil

I'm trying to create two tableViews in one UIViewController. But when I'm trying to assign value to UILabel, getting an error: fatal error: unexpectedly found nil while unwrapping an Optional value
I wonder why, I have almost the same code for TableViewController with one tableView and it works with no issues. It looks like these UI Labels are not initialized when trying to assign value to it. But don't understand how to fix it.
It fails here:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell?
if tableView == self.guestsTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell
if let guestsTable = guestsTableFetchedResultsController?.object(at: indexPath) {
print(guestsTable.guestName) // works fine, prints the value
print(cell.guestNameLabel.text) //fails here with error fatal error: unexpectedly found nil while unwrapping an Optional value
cell.guestNameLabel.text = guestsTable.guestName
cell.openTimeLabel.text = String(describing: guestsTable.openTime)
cell.cellDelegate = self
}
}
else if tableView == self.ordersTableView {
cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath)
//to be done
}
// Configure the cell...
return cell!
}
Full code of this class:
import UIKit
import CoreData
class TableUIViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CellWithButtonDelegate {
//The following two variables will not be nil because prepare for segue will set them
var tableName: String?
var table: TablesTable? = nil
fileprivate var currentTableSession: TableSessionTable? {
get {
let tableSessionTable = TableSessionTable()
return tableSessionTable.getCurrentTableSession(table: table!)
}
}
fileprivate var guestsTableFetchedResultsController: NSFetchedResultsController<GuestsTable>?
fileprivate var ordersTableFetchedResultsController: NSFetchedResultsController<OrdersTable>?
#IBOutlet weak var tableNameLabel: UILabel!
#IBOutlet weak var tableCapacityLabel: UILabel!
#IBOutlet weak var tableCountOfGuestsLabel: UILabel!
#IBOutlet weak var tableDescriptionTextView: UITextView!
#IBAction func closeTableButtonPressed(_ sender: UIButton) {
}
#IBOutlet weak var guestsTableView: UITableView!
#IBOutlet weak var ordersTableView: UITableView!
#IBAction func addGuestButtonPressed(_ sender: UIButton) {
let guestsTable = GuestsTable()
let tablesTable = TablesTable()
let table = Table(tableName: tableName!, tableCapacity: 0, locationX: nil, locationY: nil, tableImage: nil)
try? guestsTable.addNewGuest(table: tablesTable.getOrCreateTable(table: table))
updateUI()
}
#IBAction func addOrderButtonPressed(_ sender: UIButton) {
}
override func viewDidLoad() {
guestsTableView.dataSource = self
guestsTableView.delegate = self
guestsTableView.register(GuestAtTableTableViewCell.self, forCellReuseIdentifier: "guestCell")
ordersTableView.dataSource = self
ordersTableView.delegate = self
ordersTableView.register(UITableViewCell.self, forCellReuseIdentifier: "orderCell")
updateUI()
}
func didPressButton(table: TablesTable) {
}
private func updateUI () {
let tableView = guestsTableView
let context = AppDelegate.viewContext
let request : NSFetchRequest<GuestsTable> = GuestsTable.fetchRequest()
request.predicate = NSPredicate(format: "table= %#", currentTableSession!)
request.sortDescriptors = [NSSortDescriptor(key: "guestName", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
guestsTableFetchedResultsController = NSFetchedResultsController<GuestsTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? guestsTableFetchedResultsController?.performFetch()
tableView?.reloadData()
}
private func updateUI1 () {
let tableView = ordersTableView
let context = AppDelegate.viewContext
let request : NSFetchRequest<OrdersTable> = OrdersTable.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "menuItem", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
ordersTableFetchedResultsController = NSFetchedResultsController<OrdersTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? ordersTableFetchedResultsController?.performFetch()
tableView?.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell?
if tableView == self.guestsTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell
if let guestsTable = guestsTableFetchedResultsController?.object(at: indexPath) {
print(guestsTable.guestName) // works fine, prints the value
print(cell.guestNameLabel.text) //fails here with error fatal error: unexpectedly found nil while unwrapping an Optional value
cell.guestNameLabel.text = guestsTable.guestName
cell.openTimeLabel.text = String(describing: guestsTable.openTime)
cell.cellDelegate = self
}
}
else if tableView == self.ordersTableView {
cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath)
//to be done
}
// Configure the cell...
return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.guestsTableView {
return guestsTableFetchedResultsController?.sections?.count ?? 1
}
else if tableView == self.ordersTableView {
return ordersTableFetchedResultsController?.sections?.count ?? 1
}
else {return 1}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.guestsTableView {
if let sections = guestsTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].numberOfObjects
}
else {
return 0
}
}
else if tableView == self.ordersTableView {
if let sections = ordersTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].numberOfObjects
}
else {
return 0
}
}
else {return 0}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if tableView == self.guestsTableView {
if let sections = guestsTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].name
}
else {
return nil
}
}
else if tableView == self.ordersTableView {
if let sections = ordersTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].name
}
else {
return nil
}
}
else {return nil}
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if tableView == guestsTableView {
return guestsTableFetchedResultsController?.sectionIndexTitles
}
else {
return ordersTableFetchedResultsController?.sectionIndexTitles
}
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if tableView == guestsTableView {
return guestsTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
}
else if tableView == ordersTableView {
return ordersTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
}
else {return 0}
}
}
And full code of UITableViewCell class:
import UIKit
class GuestAtTableTableViewCell: UITableViewCell {
weak var cellDelegate: CellWithButtonDelegate?
#IBOutlet weak var guestNameLabel: UILabel!
#IBOutlet weak var openTimeLabel: UILabel!
#IBAction func didPressButton(_ sender: UIButton) {
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I guess you have a xib for your UITableViewCell register the xib instead of the class.
Use the following:
guestsTableView.register(UINib.init(nibName: "GuestAtTableTableViewCell", bundle: nil), forCellReuseIdentifier: "guestCell")
As you have created a prototype cell in the storyboard itself you should select the cell in the storyboard and set its identifier there. Next remove the register line from your code for guestCell. It should work

How to save the google Placepicker locations to tableviewcell?

I have written this code, i want the picker location to be saved to tableview cells every time i pick the place from place picker.
As of now only one place, i am able to save. I have tried searching in google and other things didn't get correct one. Can anyone help me with this.
class SetLocationLocationsVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
#IBOutlet weak var setLocationImagePin: UIImageView!
#IBOutlet weak var ClearLabel: UILabel!
#IBOutlet weak var DeleteButton: UIButton!
#IBOutlet weak var SetLocationView: UIView!
#IBOutlet weak var PickUpLocationButton: UIButton!
#IBOutlet weak var AddressTextField: UITextField!
#IBOutlet weak var LocationButton: UIButton!
#IBOutlet weak var NameLabel: UILabel!
#IBOutlet weak var tableViewForLocations: UITableView!
var placesClient: GMSPlacesClient!
override func viewDidLoad() {
super.viewDidLoad()
placesClient = GMSPlacesClient.shared()
AddressTextField.isHidden = true
tableViewForLocations.delegate = self
tableViewForLocations.dataSource = self
AddressTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
#IBAction func PickPlace(_ sender: Any) {
let center = CLLocationCoordinate2D(latitude: 37.788204, longitude: -122.411937)
let northEast = CLLocationCoordinate2D(latitude: center.latitude + 0.001, longitude: center.longitude + 0.001)
let southWest = CLLocationCoordinate2D(latitude: center.latitude - 0.001, longitude: center.longitude - 0.001)
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let config = GMSPlacePickerConfig(viewport: viewport)
let placePicker = GMSPlacePicker(config: config)
AddressTextField.isHidden = false
placePicker.pickPlace(callback: {(place, error) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let place = place {
self.NameLabel.text = place.formattedAddress?.components(separatedBy: ", ")
.joined(separator: "\n")
} else {
self.NameLabel.text = ""
}
})
}
#IBAction func SetLocationClicked(_ sender: Any) {
tableViewForLocations.reloadData()
AddressTextField.isHidden = true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewForLocations.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! LocationCell
cell.Label1.text = AddressTextField.text
cell.Label2.text = NameLabel.text
tableViewForLocations.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
return cell
}
#IBAction func DeleteClicked(_ sender: Any) {
}
}

show camera roll in a collectionView

I know that this question has answered in this site But I tried the code and receive an Error please help me to show my camera roll in a collection view (I know that I have to add photo usage in info.plist please Just focus on my codes thanks!!!)
here is my view controller code
class translateViewController: UIViewController , UINavigationControllerDelegate , UIImagePickerControllerDelegate , UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet var myimageView: UIImageView!
#IBAction func importImage(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image , animated: true)
{
}
}
#IBOutlet weak var cameraRollCollectionView: UICollectionView!
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
myimageView.image = image
}
else {
print("Error")
}
self.dismiss(animated: true, completion: nil)
}
#IBOutlet var translatebackgroundimg: UIImageView!
#IBOutlet var translatefrontimg: UIImageView!
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult<AnyObject>!
var assetThumbnailSize: CGSize!
override func viewDidLoad() {
super.viewDidLoad()
let fetchOptions = PHFetchOptions()
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let first_Obj:AnyObject = collection.firstObject{
//found the album
self.assetCollection = first_Obj as! PHAssetCollection
}
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = CGRect(x: self.translatebackgroundimg.frame.origin.x, y: self.translatebackgroundimg.frame.origin.y, width: self.translatebackgroundimg.frame.size.width, height: self.translatebackgroundimg.frame.size.height)
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.translatebackgroundimg.addSubview(blurView)
// Do any additional setup after loading the view.
translatefrontimg.image = UIImage(named: "Translate.png")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = translatebackgroundimg.bounds
translatebackgroundimg.addSubview(blurView)
translatebackgroundimg.frame = self.view.bounds
}
override func viewWillAppear(_ animated: Bool) {
// Get size of the collectionView cell for thumbnail image
if let layout = self.cameraRollCollectionView!.collectionViewLayout as? UICollectionViewFlowLayout{
let cellSize = layout.itemSize
self.assetThumbnailSize = CGSize(width: cellSize.width, height: cellSize.height)
}
//fetch the photos from collection
self.photosAsset = (PHAsset.fetchAssets(in: self.assetCollection, options: nil) as AnyObject!) as! PHFetchResult<AnyObject>!
self.cameraRollCollectionView!.reloadData()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
var count: Int = 0
if(self.photosAsset != nil){
count = self.photosAsset.count
}
return count;
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraCell", for: indexPath as IndexPath)
//Modify the cell
let asset: PHAsset = self.photosAsset[indexPath.item] as! PHAsset
PHImageManager.default().requestImage(for: asset, targetSize: self.assetThumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
if result != nil {
cameraCell.userImage.image = result
}
})
return cell
}
// MARK: - UICollectionViewDelegateFlowLayout methods
func collectionView(collectinView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 4
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1
}
// UIImagePickerControllerDelegate Methods
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker.dismiss(animated: true, completion: nil)
}
and here is my CollectionViewCell codes
class cameraCell: UICollectionViewCell , UIImagePickerControllerDelegate {
#IBOutlet weak var userImage: UIImageView!
func configurecell(image: UIImage){
userImage.image = image
}
}

the table view cells not release from memory

i hope all is well
i have an issue with cell in table view it's not releasing when i scrolling in table view the following is my code
class MovieDownloadedTableViewCell: UITableViewCell {
#IBOutlet weak var movieImageView: UIImageView!
#IBOutlet weak var movieNameLabel: UILabel!
#IBOutlet weak var movieEmptyCircleImageView: UIImageView!
#IBOutlet weak var movieCheckMarkImageView: UIImageView!
var fetchURL:URL? {
didSet {
updateUI()
}
}
func updateUI() {
movieImageView.image = nil
movieNameLabel.text = nil
movieEmptyCircleImageView.isHidden = true
movieCheckMarkImageView.isHidden = true
if let fetchURL = fetchURL {
if fetchURL.pathExtension == "" {
movieImageView.image = UIImage(named: "folder")
}else{
movieImageView.image = UIImage(named: "movie")
}
movieNameLabel.text = fetchURL.lastPathComponent
}// end the if let
}// end the func updateUI
}// end the class
the above my custom table view cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? MovieDownloadedTableViewCell
// defining the cell
if let cell = cell {
cell.tintColor = UIColor.white
cell.fetchURL = operationDocumentDirectoryObject.arrayOfMovieURL?[indexPath.row]
if isShowToolBar {
if operationDocumentDirectoryObject?.arrayOfShowAllEmptyCircle?[indexPath.row] == indexPath.row {
cell.movieEmptyCircleImageView.isHidden = false
}// end the if
if operationDocumentDirectoryObject.dictionaryHoldIndexCellForDisplayWhichCellSelected.count > 0 {
if operationDocumentDirectoryObject.dictionaryHoldIndexCellForDisplayWhichCellSelected[indexPath.row] == indexPath.row {
cell.movieEmptyCircleImageView.isHidden = true
cell.movieCheckMarkImageView.isHidden = false
}
}
}// end if for the isShowVar
}// end the creating cell
return cell!
}
please where my wrong code how can i make my table view cell when i scrolling is releasing from memory every time
thank you very much