0

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.

jwknz
  • 6,598
  • 16
  • 72
  • 115

1 Answers1

0

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.

hypercrypt
  • 15,389
  • 6
  • 48
  • 59
WendiKidd
  • 4,333
  • 4
  • 33
  • 50
  • Yeah your explanation makes sense, in practice I figured it out, but didn't really understand it. As for the meaning of scope - this is an iPhone app, sorry maybe I wasn't clear and the scope feature I referring to is related to the search bar. The scope allows you to set like a condition of the all the results. – jwknz Jun 30 '12 at 05:51
  • Okay, so what do you still need help with? :) – WendiKidd Jun 30 '12 at 05:53
  • Well I need the right result to come under the right scope. So when I press the name (button) and fill in a name I want the name to come up and when I type in the price I want the price to come up when the price (button) in pressed. At the moment it only shows me whatever the last cell.textlabel.text is set to as you explained earlier. – jwknz Jun 30 '12 at 05:55
  • So ideally I would set it up with an if statement, like if (scope == @"name") then show the names, but I can't use scope within the part i got, because it is not globally declared. – jwknz Jun 30 '12 at 05:57
  • Can you not pass the `scope` string to the function where your first chunk of code is being called? – WendiKidd Jun 30 '12 at 06:12
  • Also, don't use `==` for objects unless you are testing that they are the same object (not just the same value). Use `isEqual:` or `isEqualToString:`, the latter is best if you know that they are both `NSString`s. – hypercrypt Jun 30 '12 at 07:14