I have this code that where I would normally use one line:
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"Configuring cell to show search results");
shoppingList = [self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = shopList.name;
cell.textLabel.text = shopList.type;
cell.textLabel.text = shopList.price;
cell.textLabel.text = shopList.occasion;
}
The reason I have this is that I am implementing a scope under my search bar. Now the scope works, when I type in a name I would expect to see the name when I select the first scope which is the name of the product.
What is happening however is that it shows what is under occasion, since it is the last one in the list (I pressume). So when I type in the name of a product it comes up with the matching occasion instead of the name of the product.
How do I set it up that it shows what I select in the scope?
I have implemented the following method, to make the scope work correctly, just the labels are not coming up right, which is affected in the tableView section code above.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
Any help is great:-) Thanks.
The =
operator is the assignment
operator. That means that whatever is on the left side of the =
operator is going to become whatever is on the right side after that line of code is executed.
For example:
int i = 5;
i = 4;
i = 3;
i = 8;
After all four of those lines are executed, i
is equal to 8
. It doesn't matter that it was another value previously--the =
operator obliterates whatever was in the variable before and places the new value into it.
So, regardless of what you want your code to do, what you are doing is obliterating the value of cell.textlabel.text
every time you use the =
operator. shopList.name
, shopList.type
, and shopList.price
don't exist anymore after you set cell.textlabel.text
to shopList.occasion
, for the same reason i = 8
in my above example. This is a fundamental and hugely important programming concept.
Do you understand what's wrong, now? If you want to save all four items--price, name, type, and occasion--you need four separate variables to do so.
As an aside, I'm fairly certain you aren't using the word scope
properly (unless I'm entirely misunderstanding the purpose of your program). From Wikipedia:
In computer programming, a scope is the context within a computer program in which a variable name or other identifier is valid and can be used, or within which a declaration has effect. Outside of the scope of a variable name, the variable's value may still be stored, and may even be accessible in some way, but the name does not refer to it; that is, the name is not bound to the variable's storage.