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 trialVikram Pal
Courses Plus Student 101 Points"Invisible" Row Crashes When Pressed
How do I get rid of the invisible cell that crashes my application in my EditFriendsViewController?
So I have the Ribbit application down. Currently transforming it into a personal masterpiece just for fun and learning purposes.
I have a search feature added to search for usernames instead of showing everyone signed up.
But, in my edit friends screen I have the first cell which is invisible and if perchance you were to accidentally press it it crashes the application.
I believe the problem is in my search code, but I can't seem to fix it myself.
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// Dismiss The Keyboard
[self.searchBar resignFirstResponder];
// Take White Space Off End Of Text
NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Make Sure It Is Not Empty And Query Parse For Username In Field
if (![searchText isEqualToString:@""]) {
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:searchText];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Check To See If A User Is Actually Found
if (objects.count > 0) {
// Return The User Found
self.allUsers = objects;
//set the user as the table views data source and reload the table view
// Error Message If Username Is Not Found
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Username Not Found" message:@"Try again with a valid username" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
}
[self.tableView reloadData];
} else {
}
}];
}
}
I have also tried to change the return numbers to 0. The invisible cell disappears but then the search results will not display.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.user) {
//a user was found, we need to display one row.
return 1;
} else {
//a user was not found/searched for yet, dont display any rows
return 0;
}
// Return the number of rows in the section.
return [self.allUsers count];
}
Any help on this would be highly appreciated! Amit Bijlani , Ben Jakuben
6 Answers
Stone Preston
42,016 Pointstry adding some checks in numberOfSectionsInTableView. you only want 1 section if a user was found, and 0 if not.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (self.user) {
//a user was found, we need to display one section.
return 1;
} else {
//a user was not found/searched for yet, dont display any rows/sections
return 0;
}
}
Stone Preston
42,016 Pointsalso you have one property called self.user and another called self.AllUsers. You need to be using self.user in your tableView delegate methods, not self.allUsers. you need to change this line here:
// Check To See If A User Is Actually Found
if (objects.count > 0) {
// Return The User Found
self.allUsers = objects;
//set the user as the table views data source and reload the table view
// Error Message If Username Is Not Found
}
to
// Check To See If A User Is Actually Found
if (objects.count > 0) {
// Return The User Found
self.user = [objects objectAtIndex:0];
// Error Message If Username Is Not Found
}
and also make sure all your tableView delegate methods use self.user as well, not self.allUsers. They query only finds one user that matches the username.
Vikram Pal
Courses Plus Student 101 PointsAh I see what you mean now. I changed all those but this gave rise to new problems. The edit friends screen crashes the application once opened.
Stone Preston
42,016 Pointshmm it looks like the last line in this method here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.user) {
//a user was found, we need to display one row.
return 1;
} else {
//a user was not found/searched for yet, dont display any rows
return 0;
}
// Return the number of rows in the section.
return [self.allUsers count];
}
needs to be removed. It could be causing some issues. try removing it so that you only have:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.user) {
//a user was found, we need to display one row.
return 1;
} else {
//a user was not found/searched for yet, dont display any rows
return 0;
}
}
John Carr
Courses Plus Student 1,984 PointsNot entirely sure but Have you this code after - return [self.allUsers count]; } ????
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
return cell;
Vikram Pal
Courses Plus Student 101 PointsStill no fix even when I implement this. Hmm.
John Carr
Courses Plus Student 1,984 PointsDid you set identifier Cell in editFriendsViewController? for the prototype cell
Vikram Pal
Courses Plus Student 101 PointsI actually have that exact code implemented in my code already
John Carr
Courses Plus Student 1,984 Pointsis it here? if (![searchText isEqualToString:@"%@", username]) Pretty new here and to objective c, just a thought
Vikram Pal
Courses Plus Student 101 PointsVikram Pal
Courses Plus Student 101 PointsAttempted that. The invisible cell is still there and crashes when pressed.