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
}
Related
As the title said; I want to read the data from multiple UITextFields for each cell and store them in an array. How would I do so?
I have created a subclass CustomCell that has 2 UITextFields in it. P/S: the identifier for the cell is also CustomCell
Many thanks
class TableViewController : UITableViewController, UITextFieldDelegate {
var data = [input]()
#IBOutlet var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
customCell.input1.tag = indexPath.row
customCell.input2.tag = indexPath.row
customCell.input1.delegate = self
customCell.input2.delegate = self
return customCell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
#IBAction func addButton(_ sender: Any) {
table.beginUpdates()
table.insertRows(at: [IndexPath(row: data.count-1, section: 0)], with: .automatic)
table.endUpdates()
}
func textFieldDidEndEditing(_ textField: UITextField) {
let indexPath = IndexPath(row: textField.tag, section: 0)
if let customCell = self.table.cellForRow(at: indexPath) as? CustomCell{
let a = customCell.Credit.text!.isEmpty ? no:String(customCell.input1.text!)
let b = customCell.letterGrade.text!.isEmpty ? no:String(customCell.input2.text!))
inputRead.append(input(string1: a, string2: b))
}
#IBAction func foo(_ sender: Any) {
if inputRead.count == 0{
return
}
//the rest of implementation
}
CustomCell class:
Import UIKit
public class CustomCell: UITableViewCell {
#IBOutlet weak var input1: UITextField!
#IBOutlet weak var input2: UITextField!
}
Update your code as follow in your view controller.
Assign textfield's delegate self to ViewController in cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
customCell.input1.delegate = self
customCell.input2.delegate = self
return customCell
}
Now implemented text field delegate in your view controller.
func textFieldDidEndEditing(_ textField: UITextField) {
guard let jobTaskCell = textField.superview?.superview as? CustomCell else {
return
}
if textField == jobTaskCell.input1 {
// Get text from textfield and store in array
} else if textField == jobTaskCell.input2 {
// Get text from textfield and store in array
}
}
Note: Following code depends on how to place textfield in your cell. So make sure you need to check this recursively by adding and removing superView
guard let jobTaskCell = textField.superview?.superview as? CustomCell else {
return
}
This simply mean that, just for when textfield inside tableview cell without any extra view:
textField.superview = contentView of TableViewCell
textField.superview?.superview = TableViewCell
I hope this will fix your issue.
This can help you to store text in an array:-
1: Add indexPath.row as a tag to your textField in cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
customCell.input1.tag = indexPath.row
customCell.input2.tag = indexPath.row
customCell.input1.delegate = self
customCell.input2.delegate = self
return customCell
}
2: In textField delegate method you can get your textField text
func textFieldDidEndEditing(_ textField: UITextField) {
let indexPath = IndexPath(row: textField.tag, section: 0)
if let customCell = self.table.cellForRow(at: indexPath) as? CustomCell {
//From here you can get your particular input1 or input2 textfield text
print(customCell.input1.text)
print(customCell.input2.text)
}
}
I have a TableView that when the user clicks it needs to show a detail view displaying the name of the row it clicked. I populate the tableView using a Json call but I can't figure out what I'm doing wrong.
This is my bits of code of ViewController
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var valueToPass:String!
#IBOutlet weak var tableView: UITableView!
// Instantiate animals class
var items = [animalObject]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
addDummyData()
}
func addDummyData() {
RestManager.sharedInstance.getRandomUser(onCompletion: {(json) in
if let results = json.array {
for entry in results {
self.items.append(animalObject(json: entry))
}
DispatchQueue.main.sync{
self.tableView.reloadData()
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! CustomTableViewCell!
let animal = self.items[indexPath.row]
cell?.label.text = animal.name
return cell! //4.
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")
// Get Cell Label
// let indexPath = tableView.indexPathForSelectedRow!
var currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
print(currentCell.textLabel?.text)
valueToPass = currentCell.textLabel?.text
print(valueToPass)
performSegue(withIdentifier: "detailView", sender: indexPath)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "detailView") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! DetailViewController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
}
}
and my detailView only has the following info
#IBOutlet weak var tooPass: UILabel!
var passedValue: String!
override func viewDidLoad() {
super.viewDidLoad()
print("DETAILS")
print(passedValue)
tooPass.text = passedValue
}
I'm not sure if the preparedToSegue is firing a little bit earlier because my terminal looks like this:
I used as a reference the following question any guidance will be appreciated
Try this for didSelectRowAt method.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
valueToPass = self.items[indexPath.row]
print(valueToPass)
performSegue(withIdentifier: "detailView", sender: indexPath)
}
Just pass the entire item in the items array.
Make the DetailViewController's passed item of type item. Then just access item.whateverPropertyRequired.
The problem is that in your cellForRow method, you are casting your cell to CustomTableViewCell type while in your didSelectRow, you are casting it as UITableViewCell. Change
var currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
in your didSelectRow to
var currentCell = tableView.cellForRow(at: indexPath)! as CustomTableViewCell
Good approach here is to use view model. Create model which contains your items and data associated with them. And at segue pass your data model. It’s bad coding, getting data right from cell on didSelect method, unless some ui working on that cell
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)
}
}
}
}
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)
}
Im trying to get the VC that corresponds with the cell of the table view to load when tapped. I am trying to set the indexPath as the selectedIndexPath and set up the segue in the collection view, then use that in the tableView but it is throwing an error. Here is the code.
import UIKit
private let reuseIdentifier = "profileCell"
class CompanyCollectionViewController: UICollectionViewController {
//MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return stuff.company.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ProfileCollectionViewCell
cell.profileImageView.image = UIImage(named: stuff.company[indexPath.row]["image"]!)
return cell
}
//MARK: - Prepare For Segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "profileSegue" {
let profileVC = segue.destinationViewController as!
MyProfileTableViewController
let cell = sender as! UICollectionViewCell
let indexPath = collectionView?.indexPathForCell(cell)
profileVC.selectedIndexPath = indexPath
}
}
}
and then
import UIKit
class MyProfileTableViewController: UITableViewController {
//MARK: - View Did Load
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - Table View Data Source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let indexPath = selectedIndexPath {
let row = stuff.company[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("myProfileCell", forIndexPath: indexPath) as! MyProfileTableViewCell
cell.heroImageView.image = UIImage(named: row["image"]!)
title = row["name"]
cell.nameLabel.text = row["name"]
cell.statusLabel.text = row["description"]
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("myProfileCell", forIndexPath: indexPath) as! MyProfileTableViewCell
cell.heroImageView.image = UIImage(named: stuff.profile["profile image"]!)
title = stuff.profile["name"]
cell.nameLabel.text = stuff.profile["name"]
cell.statusLabel.text = stuff.profile["status"]
return cell
}
}
//MARK: - Variables
var selectedIndexPath: NSIndexPath?
}
Ah I see, you are not checking if your collectionView?.indexPathForCell is returning a valid result.
if let cell = sender as? UICollectionViewCell {
// Cell is valid
if let indexPath = collectionView?.indexPathForCell(cell) {
// indexPath is valid
} else {
// indexPath is invalid
}
} else {
// cell is invalid
}
I suspect your indexPath from your table and your indexPath from your selection view are being mixed up, the code posted above should help you debug that.
Beyond that, I believe you can accomplish your intended result by:
1) Saving your selectedIndexPath in the didSelectCellAtIndexPath: method
2) Reading the selectedIndexPath in prepareForSegue instead of deriving the indexPath from the segue sender