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!
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

James Atlee
203 PointsCoding 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
42,016 Pointsbasically 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;
}

birdman
9,450 PointsBasically 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.
James Atlee
203 PointsJames Atlee
203 PointsWhen 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
42,016 PointsStone Preston
42,016 Pointsive updated my answer to show you what the table view delegate methods should look like
James Atlee
203 PointsJames Atlee
203 Pointsif (self.user) { cell.textLabel.text = self.user.username;]; } When I type that line of code in I get 10 errors
Stone Preston
42,016 PointsStone Preston
42,016 Pointssorry there was a typo in there: try
as well as making your self.user property a PFUser object, not a PFObject. my bad
James Atlee
203 PointsJames Atlee
203 PointsThanks it works!
Stone Preston
42,016 PointsStone Preston
42,016 Pointsawesome. sorry it was a little tough to get up and running, its been a while since ive looked at that code.