Reputation: 6039
Solved:
Phillip Mills' solution has solved the issue presented in this post, as outlined below.
Currently, when a user searches for a beer in the application, the beer shows up in a UITableViewCell
subclass, which displays the name of the beer and the brewery name in UILabel
instances. The cell also contains four buttons below the labels: likeButton
, tryButton
, dislikeButton
, and deleteButton
.
When a user searches the API database for a beer, the user can then save a beer to a specific category in Core Data by using one of the buttons in the cell. These saved beers will then show up in a UITableView
elsewhere in the app. I am able to successfully save and delete beers from the cells, and they do show up in the correct category's UITableView
instance. However, if a returned beer result is not saved in Core Data, I want the deleteButton
to not be shown in the cell that is populated from the JSON of the API because the app is set up for the user to save a beer, change a beer's category, or delete a beer from the search results UITableView
instance.
I have the saving, changing of a beer's category, and deleting of a beer working correctly in both the category UITableView
instances and in the search results UITableView
. My issue arises when displaying the buttons in the results UITableView
.
When results are returned from he API, I only want the deleteButton
to be displayed if there is a beer saved in Core Data that matches the returned result. I'm guessing that I need to perform this comparison in cellForRowAtIndexPath:
, but I feel like I am going about it incorrectly because I can get the deleteButton
to either be visible or be hidden in all cells, regardless of whether the beer is saved in Core Data or not.
Here is my current implementation:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if scopeButtonIndex == 0 {
let beerCell = tableView.dequeueReusableCellWithIdentifier("foundBeerResultCell", forIndexPath: indexPath) as! BeerTableViewCell
let beer = foundBeerResults[indexPath.row]
beerCell.beerNameLabel.text = beer.name
beerCell.breweryNameLabel.text = beer.brewery
beerCell.delegate = self
for savedBeer in savedBeers {
if beer.name == savedBeer.beerName && beer.brewery == savedBeer.beerBrewery {
beerCell.deleteButton.hidden = false
} else if beer.name != savedBeer.beerName && beer.brewery != savedBeer.beerBrewery {
beerCell.deleteButton.hidden = true
}
}
return beerCell
}
}
Currently savedBeers
in an array of the saved beers in Core Data. As you can see, I am taking each beer that is returned from the API and saved in foundBeerResults
, which populates the results UITableView
instance. I am then looping through savedBeers
to see if each returned beer in foundBeerResults
matches the saved beer. If the information matches, I want the deleteButton
to be visible so that the user can delete the saved beer directly from the search results. If a returned beer result does not match a currently saved beer, I want the deleteButton
to be invisible.
Am I incorrect by assuming that I should not be iterating through arrays in cellForRowAtIndexPath:
? It seems inefficient. However, I am not sure of how to solve this problem.
Upvotes: 0
Views: 28
Reputation: 31016
Looping might or might not be a performance problem. You can measure for that but let's assume "not" for the moment since it seems like that would be a fairly small array you're using.
I think your problem is that you're not stopping the loop once you get a right answer.
How about:
beerCell.deleteButton.hidden = true
for savedBeer in savedBeers {
if beer.name == savedBeer.beerName && beer.brewery == savedBeer.beerBrewery {
beerCell.deleteButton.hidden = false
break
}
}
Upvotes: 1