Database Reference
In-Depth Information
name if the other is not present. Finally, if none are present, we set the name variable to the string
Undefined .
We then check to see whether the birthday property on our CloseFriend managedObject is set. If
it is, we use the static date formatter to get a string representation of the date and assign it to our
birthday variable. Then we assign our birthday variable to the cell's detailTextLabel text property.
Finally, we check to see whether our CloseFriend object has an image. If it does, we convert the
NSData that we do have stored in the object to a UIImage by calling the imageWithData: class method
for UIImage . If we don't have an image, we set the image to nil. Setting the cell's image property is
not required, but it is a secondary means for us to make sure that there is no image set when a cell
is dequeued.
We still need to write our removeCloseFriend: method so we can add that now.
-(void)removeCloseFriend:(CloseFriend *)closeFriend {
NSMutableArray *closeFriends = _closeFriends.mutableCopy;
[closeFriends removeObject:closeFriend];
_closeFriends = closeFriends;
[[AppDelegate managedObjectContext] deleteObject:closeFriend];
NSError *error = nil;
if(![[AppDelegate managedObjectContext] save:&error]){
NSLog(@"There was an error deleting data - %@",error.localizedDescription);
}
}
We start by creating a mutable copy of our closeFriends class property so we can modify it. Then
we remove the closeFriend object that we passed into this method. Then we assign our local
closeFriends mutable array to our closeFriends class property so that we now have an NSArray
with the object removed that we wanted to remove. The last bit is Core Data specific. We first call
deleteObject: against our manageObjectContext and pass it the object we want to delete. Then we
call the save: method on our managedObjectContext to save this change. This method returns a BOOL ,
so we just check to see if it was not successful. If it wasn't, we log our error for later debugging.
Now we need to add our Table View Delegate Methods. In our case we will only be adding one, but
there are quite a number of them at your disposal should you have a need for any of them.
#pragma mark - UITableViewDelegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_selectedFriend = _closeFriends[indexPath.row];
_shouldStartEditing = NO;
[self performSegueWithIdentifier:@"ToCloseFriendDetails" sender:nil];
}
In this method we simply set the property selectedFriend by getting the object at the index
passed to us. We set shouldStartEditing to NO, similar to what we did in the Collection View.
Then we call our segue by calling the performSegueWithIdentifier:sender: method and pass
ToCloseFriendDetails as the identifier.
 
Search WWH ::




Custom Search