1

I need to transfer data to another TableView at the click of a button, but I don’t understand how to do it

You need to pass the data to another TableViewController and save it in a variable.

I must pass an array of data

import UIKit

class DivanCell: UITableViewCell {
    
    var favFurniture: Furniture?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    @IBOutlet weak var divanNameLabel: UILabel!
    @IBOutlet weak var divanPhoto: UIImageView! 
    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var sizeDivan: UILabel!
    @IBOutlet weak var favButton: UIButton!
    @IBAction func favTapped(_ sender: Any) {
        favButton.setImage(UIImage(systemName: "heart.fill"), for: UIControl.State.normal)
        favTapped()
    }
    
    func favTapped() {
        favFurniture = (Furniture(name: divanNameLabel.text!, type: "Divan", image: divanPhoto.image!, price: priceLabel.text!))
        FavouriteTableViewCell().favFurniture = favFurniture
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }

}

into this tableview


class FavouriteTableViewCell: UITableViewCell {
    
    var favFurniture = Furniture?

    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
    }

}
Eugene
  • 65
  • 1
  • 8
  • Don't copy contents of the **view** (the cells). Pass the data from the **model** (the data source array) in one **controller** to the other. – vadian Jun 21 '20 at 09:53
  • Can I have more details, please, I'm a newbie – Eugene Jun 21 '20 at 10:03
  • Please see https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – vadian Jun 21 '20 at 10:05

2 Answers2

0

This example here does work and should give you and understanding of how to do so:

class First {
    var a = "a"
    var b = "b"
    var c = "c"
    
    init() {
        self.getsTapped(second: Second())
    }
    
    func getsTapped(second: Second) { // call this function on your tapped object
        print(second.data) // will show you the empty array
        second.data = [self.a,self.b,self.c]
        print(second.data) // now that array contains values
    }
    
}


public class Second {
    var data: Array<String> = []
}

let first = First()

You create a function which you call where-ever you like. I did so in the init() you probably want to call it on your button when pressed. Pass it the reference of the second class (which you want to pass the values to) and then let it do the magic. Note: I did create a new instance of the second class when calling the function. This will obviously not work for you. You need to initiate an instance outside so you have the reference of that class and can access it elsewhere.

Simon
  • 1,754
  • 14
  • 32
  • Still not my decision. but tell me, is it possible to copy the entire row to another tableview? That is, it’s straight to take a string and wrap it? – Eugene Jun 21 '20 at 10:54
  • func favTapped(fav: FavouriteTableVC) { favFurniture = (Furniture(name: divanNameLabel.text!, type: "Divan", image: divanPhoto.image!, price: priceLabel.text!)) fav.favFurniture = favFurniture } here is a mistake // Cannot assign value of type 'Furniture?' to type '[Furniture]' – Eugene Jun 21 '20 at 11:18
  • wrap it in ```if let furniture = Furniture { ```// now use furniture instead of Furniture whenever referencing Furniture and do your things inside here. Also it seems that it is expecting an array but you are giving it an instance of it. You could wrap it in [ ] to make an array out of it. ```}``` – Simon Jun 21 '20 at 11:22
  • I find it difficult to understand what they write to me) please, can you correct my code so that it works? – Eugene Jun 21 '20 at 11:33
0

As far as I can tell your issue is here:

func favTapped() {
        favFurniture = (Furniture(name: divanNameLabel.text!, type: "Divan", image: divanPhoto.image!, price: priceLabel.text!))
        FavouriteTableViewCell().favFurniture = favFurniture
    }

It seems that you did not understand the fundamentals of OOP.

Short answer:

You are calling this method and initialising a new FavouriteTableViewCell inside of it. How do you plan on using this instance that you just created outside of this method? Or perhaps it is not necessary to access it again? Then I'm mistaken and you can proceed like this. The changes to that instance should be visible inside of this method.

If you plan on using/ accessing the altered instance outside of your function then you need to pass your method the reference of that instance. Like that it can alter the referenced instance and you can then, after the adjustments, "see" and use these changes outside of this method.

My example above should make it clear on how to proceed.

Here is an example of your code that does compile: Note: I did not try to run it as I didn't feel like creating all necessary dependencies.

class DivanCell: UITableViewCell {
    
    var favFurniture: Furniture?

    @IBOutlet weak var divanNameLabel: UILabel!
    @IBOutlet weak var divanPhoto: UIImageView!
    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var sizeDivan: UILabel!
    @IBOutlet weak var favButton: UIButton!
    @IBAction func favTapped(_ sender: Any) {
        favButton.setImage(UIImage(systemName: "heart.fill"), for: UIControl.State.normal)
        favTapped()
    }
    
    func favTapped() {
        favFurniture = (Furniture(name: "name", type: "Divan", image: "image", price: "price"))
        
        // As you create the instance of FavouriteTableViewCell in here it will also only be visible in here
        FavouriteTableViewCell().favFurniture = favFurniture
    }
    
    func favTappedNew(favTableViewCell: FavouriteTableViewCell) {
        favFurniture = (Furniture(name: "name", type: "Divan", image: "image", price: "price"))
        
        // Now the instance of FavouriteTableViewCell that you passed to this method is altered and you can access it outside
        favTableViewCell.favFurniture = favFurniture
    }

}

class FavouriteTableViewCell: UITableViewCell {
    
    var favFurniture:Furniture?

}

class Furniture {
    var name: String
    var type: String
    var image: String
    var price: String
    
    init(name: String, type: String, image: String, price: String) {
        self.name = name
        self.type = type
        self.image = image
        self.price = price
    }
}

Simon
  • 1,754
  • 14
  • 32