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

Displaying an NSArray on a pre-populated table view

Hello guys, So for my app I have a text field and a search button. The user enters a username and I want it to be displayed on the 'usersTable'. This is the code for that:

- (IBAction)searchButtonPressed:(id)sender {
    NSLog(@"search button pressed");
    NSString *searchedText = _searchField.text;
    //Queries for users.
    PFQuery *filterQuery = [PFQuery queryWithClassName:@"_User"];
    [filterQuery setLimit:1000];
    [filterQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if (!error) {
            // Here you can store fetched data of parse to your array.
            NSArray *filteredData;
            filteredData = [NSArray arrayWithArray:objects];

        }
    }];
     NSArray *filteredData;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchedText];
     NSArray *newArray = [filteredData filteredArrayUsingPredicate:predicate];



}

However, I've got no idea at all if this would work as a filtering system and on how I would populate my 'usersTable' which is a UITableView with the array NSArray. The table is already populated and I want to remove everything that is already their and display the results in the NSArray, newArray. Could anybody please approve this code and tell me how I'd remove, and then populate with the new array?

Thanks for your help,

Arman

1 Answer

Assign the new array to your main array and then call [tableView reloadData] to have everything redrawn again.