Reputation: 249
I have product tags stored in the the following variable:
var productTags : [String : [String]] = [:]
I also have a search bar for my tableView where I populate the following variable based on what the user is searching for from the productTags:
var searchResults: [String : [String]] = [:]
So the content of the these two variables would be something like this:
productTags = [ product1 : [tag1, tag2, tag3, tag4],
product2 : [tag1, tag2, tag3, tag4, tag 5],
product3 : [tag1, tag2, tag3]
]
// similar for the searchResults depending on the search
Now I'd like to populate only the tag1
of each product into the tableView. How do I go about doing this?
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return //what should I be returning here?
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.HomeSearchCell, for: indexPath) as! HomeSearchCell
cell.productName?.text = //how can I populate the "tag1" of the product in here? so the label shows the tag1 as the search result
return cell
}
I also want to make sure I can track which tag specifically the user is clicking on so I can track back the tag to the product ID. For example, if you search tag1
of product2
and then you see tag1
in the search result in the table view and click on it; how can I get the product ID which in this case is product2
from the row you selected on?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// what should I put here to get the productID of the tag/row selected by the user from the searchresults?
}
EDIT:
This is how I populate my productTags
:
db.collection(DatabaseRef.searchTags).document(DatabaseRef.productTags).getDocument { snapshot, error in
guard error == nil, let snapshot = snapshot else {
return
}
let data = snapshot.data()!
for (key, _) in data {
let productTags = data["\(key)"] as? [Any]
if let maxIndex = productTags?.count {
for index in 0..<maxIndex {
if let tag = productTags![index] as? String, tag != "" {
if self.productTags[key] == nil {
self.productTags[key] = []
self.productTags[key]?.append(tag)
} else {
self.productTags[key]?.append(tag)
}
}
}
}
}
}
and this is how I filter my productTags
to populate the searchResults
:
searchResults = productTags.filter({ (productTags) -> Bool in
if let name = productTags.value as? [String] {
for tag in name {
let isMatch = tag.localizedCaseInsensitiveContains(SearchText)
return isMatch
}
}
return false
})
Upvotes: 0
Views: 727
Reputation: 136
TableViews work best when dealing with Arrays. If possible, I would turn the dictionary into an array of Dictionary<String:[String]>
. So it would look something like this.
var productTags: [[String: [String]]] = [
[product1: [tag1, tag2, tag3, tag4]],
[product2 : [tag1, tag2, tag3, tag4, tag5]],
[product3 : [tag1, tag2, tag3]]
]
Then from there you can return the number of product dictionaries in the array
func tableView(_ tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
return productTags.count
}
And from there you can access the productTagDictionaries by indexPath.row
let productTags = productTags[indexPath.row].values.first
cell.productName?.text = productTags?.first
Upvotes: 1