Pass an array in sd_setImage - ios

How can I pass an image array from the first controller (collectionCell) to the second controller (collectionCell) using sg_setImage and class (swift file) using segue?
To load an array of images onto the first controller, I use collectionCell inside tableCell.
ViewController (firstVC):
class ViewController1: UIViewController {
#IBOutlet weak var tableView: UITableView!
var image: [Model] = []
var ref: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference(withPath: "Студии2")
ref.observe(.value, with: { (snapshot) in
var newImages: [Model] = []
for item in snapshot.children {
let object = Model(snapshot: item as! FIRDataSnapshot)
newImages.append(object)
}
self.image = newImages
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue1" {
if let indexPaths = self.tableView.indexPathForSelectedRow {
let indexPath = indexPaths as NSIndexPath
let dvc = segue.destination as! ViewController2
dvc.photo = [image[indexPath.row]]
}
}
}
}
extension ViewController1: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return image.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
cell.images = [image[indexPath.row].image,
image[indexPath.row].image2]
return cell
}
}
tableViewCell (firstVC):
class TableViewCell1: UITableViewCell {
var images = [String]()
#IBOutlet weak var collectionView: UICollectionView!
}
extension TableViewCell1: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell1
cell.imagesView.sd_setImage(with: URL(string: images[indexPath.item]))
return cell
}
}
collectionViewCell (secondVC):
class ViewController2: UIViewController {
#IBOutlet weak var collectionView: UICollectionView!
var photo: [Model] = []
var ref: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController2: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photo.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell2
let array = [photo[indexPath.item].image,
photo[indexPath.item].image2]
cell.imagesView.sd_setImage(with: URL(string: array[indexPath.item]!))
// cell.imagesView.sd_setImage(with: URL(string: photo[indexPath.item].image2))
return cell
}
}
class Model (swift file):
class Model {
var image: String!
var image2: String!
var images: [String] = []
var images2: [String] = []
var ref: FIRDatabaseReference!
init(snapshot: FIRDataSnapshot) {
ref = snapshot.ref
let value = snapshot.value as! NSDictionary
let snap1 = value["hall1"] as? NSDictionary
let snap2 = value["hall2"] as? NSDictionary
image = snap1?["qwerty"] as? String ?? ""
image2 = snap2?["qwerty"] as? String ?? ""
if let post1 = snap1 as? [String: AnyObject] {
for (_, value) in post1["images"] as! [String: AnyObject] {
self.images.append(value as! String)
}
}
if let post1 = snap1 as? [String: AnyObject] {
for (_, value) in post1["images"] as! [String: AnyObject] {
self.images2.append(value as! String)
}
}
}
}

Related

Get TableviewCells to display different data that is extracted from firebase and show them in CollectionView

I have this two structs, the data I am getting from a Firebase Database and I don't know how to get each tableview cell to display a different collection view, I have both cells (TableviewCell and CollectionViewCell) in xib Files but can't get them the way I want it to.
I have attached my ViewController where they are both in and the TableViewCell class. Please help
struct Clothes {
var price: Double
var imgClUrl: String
var id: String
var isActive: Bool = true
var timeStamp: Timestamp
var stock: Double
var name: String
init(data : [String : Any]) {
self.price = data["price"] as? Double ?? 0
self.imgClUrl = data["imgClUrl"] as? String ?? ""
self.id = data["id"] as? String ?? ""
self.isActive = data["isActive"] as? Bool ?? true
self.timeStamp = data["timeStamp"] as? Timestamp ?? Timestamp()
self.stock = data["stock"] as? Double ?? 0
self.name = data["name"] as? String ?? ""
}
}
struct TypesOfCLothing {
var title : String
var id : String
var clothes : [Clothes]
init (data : [String : Any]){
self.title = data["title"] as? String ?? ""
self.id = data["id"] as? String ?? ""
self.clothes = data["Clothes"] as! [Clothes]
}
}
class eachStoreTableViewCell: UITableViewCell{
#IBOutlet weak var eachRowCollView: UICollectionView!
#IBOutlet weak var eachRowLbl: UILabel!
var clothes = [Clothes]()
override func awakeFromNib() {
super.awakeFromNib()
eachRowCollView?.reloadData()
eachRowCollView?.delegate = self
eachRowCollView?.dataSource = self
self.eachRowCollView?.register(UINib(nibName: "RowColectionView", bundle: nil), forCellWithReuseIdentifier: Identifiers.ClothesCell)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func configureCell(clothingType: TypesOfCLothing) {
eachRowLbl?.text = clothingType.title}}
extension eachStoreTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return clothes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ClothesCell, for: indexPath) as? RowColectionView{
cell.configureCell(clothes: clothes[indexPath.item])
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 160, height: 180)
}
class EachStoreVC: UIViewController {
#IBOutlet weak var StoreTblView: UITableView!
var typesOfClothing = [TypesOfCLothing]()
var tienda: Tiendas!
let db = Firestore.firestore()
var listener : ListenerRegistration!
override func viewDidLoad() {
super.viewDidLoad()
// StoreTblView?.delegate = self
// StoreTblView?.dataSource = self
StoreTblView?.register(UINib(nibName: "eachStoreTblViewCell", bundle: nil), forCellReuseIdentifier: Identifiers.ClothingTypeCell)
StoreTblView?.cellLayoutMarginsFollowReadableWidth = false
}
override func viewDidAppear(_ animated: Bool) {
}
override func viewDidDisappear(_ animated: Bool) {
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}}
extension EachStoreVC : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return typesOfClothing.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.ClothingTypeCell, for: indexPath) as? eachStoreTableViewCell {
cell.configureCell(clothingType: typesOfClothing[indexPath.row])
cell.frame = CGRect(x: 0, y: 0, width: StoreTblView.frame.size.width, height: cell.frame.size.height)
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}}
EDIT
The collectionView and Tableview now show fine but I'm having trouble getting the data from firebase, I have an array of document references inside each TypesOfClothing document, the code is as follows. I have also updated my 2 Structs.
func getRowData(){
listener = db.collection("TypesOfClothing").addSnapshotListener({ (snap, error) in
if let error = error {
debugPrint(error.localizedDescription)
return
}
snap?.documentChanges.forEach({ (change) in
let data = change.document.data()
let typesOfClothing = TypesOfCLothing.init(data: data)
switch change.type {
case.added:
self.onDocumentAdded(change: change, category: typesOfClothing)
case.modified:
self.onDocumentModified(change: change, category: typesOfClothing)
case.removed:
self.onDocumentRemoved(change: change)
}
})
}) }
func onDocumentAdded(change: DocumentChange, category: TypesOfCLothing){
let newIndex = Int(change.newIndex)
typesOfClothing.insert(category, at: newIndex)
StoreTblView?.insertRows(at: [IndexPath(row: newIndex, section: 0)], with: UITableView.RowAnimation.left)
}
func onDocumentModified(change: DocumentChange, category: TypesOfCLothing){
if change.newIndex == change.oldIndex {
let index = Int(change.newIndex)
typesOfClothing[index] = category
StoreTblView?.reloadRows(at: [IndexPath(row: index, section: 0)], with: UITableView.RowAnimation.left)
} else {
let oldIndex = Int(change.oldIndex)
let newIndex = Int(change.newIndex)
typesOfClothing.remove(at: oldIndex)
typesOfClothing.insert(category, at: newIndex)
StoreTblView?.moveRow(at: IndexPath(row: oldIndex, section: 0), to: IndexPath(row: newIndex, section: 0))
}
}
func onDocumentRemoved(change: DocumentChange){
let oldIndex = Int(change.oldIndex)
typesOfClothing.remove(at: Int(oldIndex))
StoreTblView?.deleteRows(at: [IndexPath(row: oldIndex, section: 0)], with: UITableView.RowAnimation.fade)
}
Struct use like this:
struct TypesOfClothing {
var title : String
var clothes: [Clothes]
}
struct Clothes {
var price: Double
var imgClUrl: String
var id: String
var isActive: Bool = true
var timeStamp: Timestamp
var stock: Double
var name: String
}
in Main View controller: try code like this:
var typesOfClothing: [TypesOfClothing] = []
in numberOfRowsInSection
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return typesOfClothing.count
}
in cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.ClothingTypeCell, for: indexPath) as? eachStoreTableViewCell {
if indexPath.row < typesOfClothing?.count ?? 0 {
let cellData = typesOfClothing?[indexPath.row]
cell.configureCell(cellData)
}
return cell
}
return UITableViewCell()
}
in UITableViewCell class try like this:
class eachStoreTableViewCell: UITableViewCell{
#IBOutlet weak var eachRowCollView: UICollectionView!
#IBOutlet weak var eachRowLbl: UILabel!
var clothes = [Clothes]()
override func awakeFromNib() {
super.awakeFromNib()
eachRowCollView?.delegate = self
eachRowCollView?.dataSource = self
self.eachRowCollView?.register(UINib(nibName: "RowColectionView", bundle: nil), forCellWithReuseIdentifier: Identifiers.ClothesCell)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func configureCell(_ clothingType: TypesOfClothing) {
eachRowLbl?.text = clothingType.title
clothes = clothingType.clothes
eachRowCollView?.reloadData()
}
}
extension eachStoreTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return clothes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ClothesCell, for: indexPath) as? RowColectionView{
cell.configureCell(clothes: clothes[indexPath.item])
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 160, height: 180)
}
}
it'll help you.
You can make one structs for load tableView Data
struct ClothingData
{
var typesOfClothing : String = ""
var clothes : [Clothes] = []
}
Load this structArray for tabelView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return ClothingData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.ClothingTypeCell, for: indexPath) as? eachStoreTableViewCell
let row = ClothingData[indexPath.row]
cell.eachRowLbl?.text = row.typesOfClothing
cell.clothes = row.clothes
// Here you can pass clothesList to tabelViewCell for load collectionView
return cell
}

How can I pass data through segue from a collectionViewCell and button embedded in TableViewCell?

I wish to pass data through segue from a collectionViewCell embedded in a TableViewCell;
Also from a Button embedded in a TableViewCell
Here is the sample code :
Here is the TableViewCell's class :
class PopularCell: UITableViewCell {
#IBOutlet weak var ViewAllButton: UIButton!
#IBOutlet weak var PopularEvents: UILabel!
#IBOutlet weak var EventCollection: UICollectionView! // EMBEDDED COLLECTIONVIEW
var events = [Events]()
override func awakeFromNib() {
super.awakeFromNib()
// EMBEDDED BUTTON
ViewAllButton.setIcon(prefixText: "View All ", prefixTextColor: .blue, icon: .typIcons(.chevronRight), iconColor: .blue, postfixText: " ", forState: .normal, iconSize: 24) // EMBEDDED BUTTON
EventCollection.delegate = self
EventCollection.dataSource = self
}
extension PopularCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return events.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = EventCollection.dequeueReusableCell(withReuseIdentifier: "EventCell", for: indexPath) as! EventCell
let event = events[indexPath.row]
print("Event Name:\(event.event_name)")
cell.event = event
return cell
}
}
Here is the ViewController(TableView Delegate & DataSource) class :
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groupedEventArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 245
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PopularCell", for: indexPath) as! PopularCell
let (category, events) = groupedEventArray[indexPath.row]
cell.ViewAllButton.addTarget(self, action: #selector(VenueViewController.ViewAll(_:)), for: .touchUpInside)
cell.PopularEvents.text = category
cell.events = events
return cell
}
#objc func ViewAll(_ sender:UIButton!) {
self.performSegue(withIdentifier: "ViewEvents", sender: sender)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "ViewEvents") {
let destination = segue.destination as? EventListController
destination?.navigationItem.title = "Event Category"
}
else if(segue.identifier == "ViewEventDetails") {
if let collectionCell: EventCell = sender as? EventCell {
if let _: UICollectionView = collectionCell.superview as? UICollectionView {
let destination = segue.destination as? EventDetailViewController
destination?.navigationItem.title = "Event Details"
}
}
}
}
How do I configure the func prepare so as to pass data through the "ViewEvents" & "ViewEventDetails" segue identifiers accordingly

Pass data from collectionCell inside tableCell to secondVC use protocol

I have two viewController's. The first VC has collectionCell inside tableCell. The second VC has collectionCell, and I have swift File with Model have a class. I need to pass data from the first VC (collectionCell) to the second VC.
Pass data need from collectionCell because collectionCell has images (from swift File - Model). I think need use a protocol, but I don't really understand how to work with a protocol. Please help.
My code:
1stVC(viewController):
class ViewController1: UIViewController {
#IBOutlet weak var tableView: UITableView!
var image: [Model] = []
var ref: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference(withPath: "Студии2")
ref.observe(.value, with: { (snapshot) in
var newImages: [Model] = []
for item in snapshot.children {
let object = Model(snapshot: item as! FIRDataSnapshot)
newImages.append(object)
}
self.image = newImages
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController1: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return image.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
cell.images = [image[indexPath.row].image,
image[indexPath.row].image2]
return cell
}
}
1stVC(collectionCell inside tableCell):
protocol PostDelegate {
func selectedPost(cell: String)
}
class TableViewCell1: UITableViewCell {
var images = [String]()
var selecetdItem: Model?
var postDelegate: PostDelegate?
}
extension TableViewCell1: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell1
cell.imagesView.sd_setImage(with: URL(string: images[indexPath.item]))
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
postDelegate?.selectedPost(cell: self.images[indexPath.item])
print(images[indexPath.item])
}
}
2ndVC(viewController):
class ViewController3: UIViewController {
#IBOutlet weak var collectionView: UICollectionView!
// var images = [String]()
var images: [Model] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController3: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell3
// let array = [images[indexPath.item].images,
// images[indexPath.item].images2]
// cell.imagesView.sd_setImage(with: URL(string: array[indexPath.item]))
// cell.imagesView.sd_setImage(with: URL(string: images[indexPath.item]))
return cell
}
}
Model(swift File have a class):
class Model {
var image: String!
var image2: String!
var images: [String] = []
var images2: [String] = []
var ref: FIRDatabaseReference!
init(snapshot: FIRDataSnapshot) {
ref = snapshot.ref
let value = snapshot.value as! NSDictionary
let snap1 = value["hall1"] as? NSDictionary
let snap2 = value["hall2"] as? NSDictionary
image = snap1?["qwerty"] as? String ?? ""
image2 = snap2?["qwerty"] as? String ?? ""
if let post1 = snap1 as? [String: AnyObject] {
for (_, value) in post1["images"] as! [String: AnyObject] {
self.images.append(value as! String)
}
}
if let post1 = snap1 as? [String: AnyObject] {
for (_, value) in post1["images"] as! [String: AnyObject] {
self.images2.append(value as! String)
}
}
}
}
Pass ViewController1 as the postDelegate of TableViewCell1
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
//make VC1 as post delegate of cell
(cell as! TableViewCell1).postDelegate = self
cell.images = [image[indexPath.row].image,
image[indexPath.row].image2]
return cell
}
Finally implement
extension ViewController1 : PostDelegate {
func selectedPost(cell: String) {
//call self.perform segue or load VC2 and pass data here
}
}
Pinch of Advice :
var postDelegate: PostDelegate?
will result in strongly holding a reference to the delegate being passed. Which will result in memory leaks. To make it safer declare delegate as weak
weak var postDelegate: PostDelegate?
In order to make the Protocol weak your protocol
protocol PostDelegate: NSObjectProtocol {
func selectedPost(cell: String)
}

Can i have a CollectionView and a TableView in same View Controller

I actually have a view controller in which i am showing product attributes in a table view in the lower half of the View Controller and was showing product thumbnail in table view header. But then i realised that a product can have multiple thumbnails so i should add a collection view in upper half of view controller to show all those thumbnail (scrollable horizontally). I added both datasource and delegate for Collection view (UICollectionViewDataSource, UICollectionViewDelegate) and wrote functions to return number of section, number of rows and cellAtIndex but these functions are not called.
So my query is can i have both collection view and table view in same view controller? If yes, then how?
I am using iOS 8 SDK with swift
class ViewController: UIViewController {
#IBOutlet weak var collView: UICollectionView!
#IBOutlet weak var tblView: UITableView!
var arrMain = NSMutableArray()
var arrDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
var arrSunday = ["No Data Available"]
var arrMonday = ["1","2","3"]
var arrTuesday = ["A","B","C"]
var arrWednesday = ["a","b"]
var arrThursday = ["d","e","f"]
var arrFriday = ["5","6","7"]
var arrSaturdsay = ["X","y","z"]
override func viewDidLoad() {
super.viewDidLoad()
arrMain = (arrSunday as NSArray).mutableCopy() as! NSMutableArray
}
}
extension ViewController: UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrDays.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collView.dequeueReusableCell(withReuseIdentifier: "collCell", for: indexPath) as! collCell
cell.lblDays.text = arrDays[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 0 {
arrMain = (arrSunday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 1 {
arrMain = (arrMonday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 2 {
arrMain = (arrTuesday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 3 {
arrMain = (arrWednesday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 4 {
arrMain = (arrThursday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 5 {
arrMain = (arrFriday as NSArray).mutableCopy() as! NSMutableArray
}
else if indexPath.item == 6 {
arrMain = (arrSaturdsay as NSArray).mutableCopy() as! NSMutableArray
}
tblView.reloadData()
}
}
extension ViewController: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrMain.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tblCell
cell.lblName.text = arrMain[indexPath.row] as? String
return cell
}
}
class tblCell: UITableViewCell{
#IBOutlet weak var lblName: UILabel!
}
class collCell : UICollectionViewCell{
#IBOutlet weak var lblDays: UILabel!
}

Swift : Pass TableViewCell text to new ViewController's cell

How to pass the same text from tableVC to the detail tableVC using an array populated in tableVC.
It is working in tableVC but no data passed into the detailVC.
They share one tableviewcell.
class TableViewCell: UITableViewCell {
#IBOutlet var img: UIImageView!
#IBOutlet var title: UILabel!
}
tableVC
class TableViewController: UITableViewController {
var thecourseName = [String]()
override func viewDidLoad() {
super.viewDidLoad()
thecourseName = ["course 1 ","course 2 ","course 3 "]
theimg = [UIImage(named: "109.png")!,UIImage(named: "110.png")!]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.title.text = thecourseName[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if ( segue == "toInfo") {
var text = self.tableView.indexPathForSelectedRow()
var detailsVC: DetalisTableViewController = segue.destinationViewController as! DetalisTableViewController
detailsVC.courseName = thecourseName
}
}
detailVC
import UIKit
class DetalisTableViewController: UITableViewController {
var courseName = [String]()
#IBOutlet var passedCourse: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
// Configure the cell...
cell.title.text = courseName[indexPath.row]
return cell
}
StoryBoard
http://s18.postimg.org/mwkw5hf1l/image.png
Your issue is that you're not passing any information to your detailViewController about the contents of the selected cell. Do this instead:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if ( segue.identifier == "toInfo") {
if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
var detailsVC = segue.destinationViewController as! DetalisTableViewController
println(thecourseName[indexPath.row])
println(indexPath.row)
detailsVC.courseName = thecourseName[indexPath.row]
}
}
}
Now instead of passing the whole array of course names to DetailsTableViewController, your variable courseName just contains one:
var courseName: String = ""
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
cell.title.text = courseName
return cell
}

Resources