Reputation: 42
Trying to code for about us page using tableviews. I am able to create the first table view for "about us" components
var aboutUs = ["About us", "Contact us", "Privacy policy"]
Have to pass on this items to next table view containing aboutUsDetails
var aboutUsDetails = ["A team of docs and coders ","Contact us at [email protected]","Privacy Policy will be updated soon"]
I have created the segue function like this
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == SegueDetailsTableViewController
{
let detailViewController = segue.destination as! DetailsTableViewController
let myIndexPath = self.tableView.indexPathForSelectedRow!
let row = myIndexPath.row
detailViewController.aboutUsDetails = aboutUs[row]
}
I am a bit confused here because aboutUsDetails is a [String]; and it is not passing on? How do I overcome this?
Upvotes: 0
Views: 960
Reputation: 473
You need to only one changes and very small to below line
detailViewController.aboutUsDetails = [aboutUs[row]]
because aboutUs[row] is string and [aboutUs[row]] is Array of String and you can not assign String to aboutUsDetails which is array of String.
Upvotes: 0
Reputation: 924
aboutUs[row] is a single string, aboutUsDetails seems to be an array of strings. You can either change aboutUsDetails to be a String, or pass your data like this:
detailViewController.aboutUsDetails = [aboutUs[row]]
Upvotes: 0
Reputation: 31675
If you are trying to pass only one string, there is no need to declare aboutUsDetails
property as an array of strings, instead let it be just a single string:
In DetailsTableViewController
:
Change aboutUsDetails: [String]?
to aboutUsDetails: String?
If you are implementing the following code snippets, you should change them as follows:
Change this:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return aboutUsDetails.count
}
to this:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
And change this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// you might want to have a custom cell, you don't have to change it...
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellId")
cell?.textLabel?.text = aboutUsDetails[indexPath.row]
}
to this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// you might want to have a custom cell, you don't have to change it...
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellId")
// this is part you should foucing on...
cell?.textLabel?.text = aboutUsDetails
}
Note: you might need to do Optional Binding to display the value of aboutUsDetails
as it should (without "Optional("...")").
OR
if you insist to declare aboutUsDetails as an array of strings (which I think that there is no need to do that), you will need to pass your string as an array containing one string, as follows:
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == SegueDetailsTableViewController
{
let detailViewController = segue.destination as! DetailsTableViewController
let myIndexPath = self.tableView.indexPathForSelectedRow!
let row = myIndexPath.row
// here is the change, you are passing your string as the first
// (and the only one) element of 'aboutUsDetails'
detailViewController.aboutUsDetails = [aboutUs[row]]
}
}
Hope this helped.
Upvotes: 1