Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Coding help

In the tutorial for implementing a search function. I need help for the code that says it has found a user and needs to set the user as the table data source.

2 Answers

Stone Preston
Stone Preston
42,016 Points

basically you need to have a property for the user that was found. a PFUser property called user or whatever you want to call it. when the query is succesful you would need to set that user property

//check to make sure the query actually found a user
                if (objects.count > 0) {

                    //found a user. set the user property to the user that the query found
                   self.user = [objects objectAtIndex:0];



                } 

then in your table view delegate methods you need to use your self.user variable instead of self.allUsers or whatever you had before as the data source and make a few modifications:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections. If a user was found there will be one section, if not the table will be empty
    if (self.user) {

        return 1;

    } else {

        return 0;
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section. If a user was found there will be one row
    if (self.user) {

        return 1;

    } else {

        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (self.user) {
   cell.textLabel.text = self.user.username;
}



    return cell;
}

When i change self.allUsers to self.user in my table view delegates. I get the error: No visible @interface for 'PFObject' declares the selector 'objectAtIndex:'

Stone Preston
Stone Preston
42,016 Points

ive updated my answer to show you what the table view delegate methods should look like

if (self.user) { cell.textLabel.text = self.user.username;]; } When I type that line of code in I get 10 errors

Stone Preston
Stone Preston
42,016 Points

sorry there was a typo in there: try

if (self.user) {
   cell.textLabel.text = self.user.username;
}

as well as making your self.user property a PFUser object, not a PFObject. my bad

Thanks it works!

Stone Preston
Stone Preston
42,016 Points

awesome. sorry it was a little tough to get up and running, its been a while since ive looked at that code.

Basically you need to have a property for the user that was found. a PFObject property called user or whatever you want to call it. When the query is successful you would need to set that user property.

//Check to make sure the query actually found a user if (objects.count > 0) {

                //Found a user. set the user property to the user that the query found
               self.user = [objects objectAtIndex:0];

            } 

Then in your table view delegate methods you need to use your self.user variable instead of self.allUsers or whatever you had before as the data source.