Home > Software engineering >  Adding a cell to another TableView. (selecting an item from a tableview, showing it in another table
Adding a cell to another TableView. (selecting an item from a tableview, showing it in another table

Time:11-29

I'm building an app which which has built in with 2 different tabs. First tab is is "Home" which basically has a tableview with cells that configured from an api.(The api gets me country names for now) Those cells also have a "Star" button which prints the data of the specific cell for now.

Second tab is "Saved" tab(SavedViewController), where I want to show the "starred" countries, using a tableview.

You can see the image below in order to get an idea for the app.

App simulation Image

The star button has a function in my CountriesTableViewCell. I'm using a saveButtonDelegate in order to let the SavedViewController know about an item is going to be saved. The code in CountriesTableViewCell for star button is as below.

@objc func buttonTapped() {
    //If Button is selected fill the image. Else unfill it.
    
    if !isSaveButtonSelected {
        saveButton.setImage(UIImage(systemName: "star.fill"), for: .normal)
        isSaveButtonSelected = true
        
        saveButtonDelegate?.saveButtonClicked(with: countryData) //Checking if save button is clicked
    }
}

countryData is the data that I get from the api, and this is the data I want to pass to SavedViewController.

struct CountryData: Codable {
let name : String
}

So on the SavedViewController, I'm handling the data using the SaveButtonProtocol conformance as below:

extension SavedViewController: SaveButtonProtocol {

func saveButtonClicked(with data: CountryData) {
    countryDataArray.append(data)
    print("saveButtonClicked")
    print("countryData in savevc is \(countryDataArray)")
    DispatchQueue.main.async {
        self.countriesTableView.reloadData()
    }
}
}

Whenever I click the star button on the first tab, this function is getting called on SavedViewController. So whenever I click to button, those print statements above work fine.

The problem is, whenever the star button is clicked, it should append the data of the current clicked cell to countryDataArray in SavedViewController. But the array is not populating as it should.

Let's say I pressed the first cell's star button, my print("countryData in savevc is (countryDataArray)") statement prints : ["Vatican City"], then I press the second cell's star button it only prints ["Ethiopia"] while it should print ["Vatican City", "Ethiopia"]

Why this issue is happening? My best guess is I'm delegating SavedViewController from the cell class so it behaves differently for every other cell. If that is the problem what should I do to solve it?

Many Thanks.

CodePudding user response:

You should store your data in a shared (static array) object so you only have one source and add the saved indicator in your country struct so you do not rely on what is displayed in one view controller.

  • Related