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
George S-T
6,624 PointsNeed help.. again... Adding checkmarks when the TableView is loaded video.
Getting fed up with this. Every video I seem to fall into some problem that I can't solve even after double watching the video.
This time it's with making the checkmarks appear next to friends that are already added on the Edit Friends view.
I just can't seem to find what Im doing wrong, I will paste my EditFriendsViewController.m below:
#import "EditFriendsViewController.h"
#import <Parse/Parse.h>
#import "FriendsViewController.h"
@interface EditFriendsViewController ()
@end
@implementation EditFriendsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Sets up a query.
PFQuery *query = [PFUser query];
//Makes the query display in Ascending order of usernames.
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
//Makes the array 'allUsers' into the NSArray objects property.
self.allUsers = objects;
//Reload the data (users) within the tableView.
[self.tableView reloadData];
}
}];
//Assigning the currentUser property to the current user logged in.
self.currentUser = [PFUser currentUser];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//If the segue is showEditFriends, make the EditFriendsViewController equal the mutable array of 'friends'.
if ([segue.identifier isEqualToString:@"showEditFriends"]) {
EditFriendsViewController *viewController = (EditFriendsViewController *) segue.destinationViewController;
viewController.friends = [NSMutableArray arrayWithArray:self.friends];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//One section within the table view.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//Makes the number of rows equal to the number of objects in allUsers.
return self.allUsers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//Makes the cell text equal to the user's username.
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;
//Is the user is a friend, give them a checkmark. If not, give them no accessory.
if ([self isFriend:user]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Makes the row not highlighted after clicking on it.
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
//The cell's accessory type is a checkmark.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//Friends relation = key friendsRelation.
PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];
PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
//When a user is clicked on, add them as a friend.
[friendsRelation addObject:user];
//Save.
[self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
}];
}
#pragma mark - Helper Methods
- (BOOL)isFriend:(PFUser *)user {
for (PFUser *friend in self.friends) {
//If the objectId of the friend is the same as the user's objectId, return yes.
if ([friend.objectId isEqualToString:user.objectId]) {
return YES;
}
}
//If not, return no.
return NO;
}
@end
I run the app, login, add some friends on the edit friends screen which then show on my friends list. When I click back onto Edit friends, they don't have ticks next to their names.
4 Answers
Stone Preston
42,016 Pointscan you post the code where you set the self.friends property, it might be in prepare for segue of your friends view controller.
Stone Preston
42,016 Pointsalso put an NSLog in your viewDidAppear and log the friends array. something could be going on with that
NSLog(@"friends: %@", self.friends);
Stone Preston
42,016 Pointsalso put an NSLog in your viewDidAppear and log the friends array. something could be going on with that
NSLog(@"friends: %@", self.friends);
George S-T
6,624 Points- (void)viewDidAppear:(BOOL)animated {
NSLog(@"%@", self.friends);
}
Returning (null)
Stone Preston
42,016 Pointsok so thats why the checkmarks are not appearing. your self.friends array is null so your isFriend method doesnt do anything.
if ([segue.identifier isEqualToString:@"showEditFriends"]) {
EditFriendsViewController *viewController = (EditFriendsViewController *) segue.destinationViewController;
viewController.friends = [NSMutableArray arrayWithArray:self.friends];
}
this looks correct, it should set the friends array in your edit friends view controller. However the array you pass to arrayWithArray could be null for some reason so lets log the friendsViewController self.friends array like so:
if ([segue.identifier isEqualToString:@"showEditFriends"]) {
NSLog(@"friendsViewController friends: %@", self.friends);
EditFriendsViewController *viewController = (EditFriendsViewController *) segue.destinationViewController;
viewController.friends = [NSMutableArray arrayWithArray:self.friends];
}
add the NSLog to prepare for segue then test your app and trigger the segue to edit friends and see whats going on.
George S-T
6,624 PointsGeorge S-T
6,624 PointsYou mean this? @property (nonatomic, strong) NSMutableArray *friends;
Stone Preston
42,016 PointsStone Preston
42,016 PointsNo, where does that property actually get set. It might be set in the prepareForSegue of your friends view controller I'm not sure
George S-T
6,624 PointsGeorge S-T
6,624 Points