Database Reference
In-Depth Information
We start by allocating and initializing an NSFetchRequest object. Then we create our
NSEntityDescription by passing the string CloseFriend and our current NSManagedObjectContext to
the method entityForName:inManagedObjectContext . Then we set the entity on our request to the
entity we just created.
We want our list sorted by last name so we put one NSSortDescriptor in an array and pass that into
our request using the method setSortDescriptors: .
The final thing is to execute the fetch request against our managed object context by calling
executFetchRequest:error: . Remember that for the error we are passing a pointer to a pointer
instead of a pointer to an object. This means that if an error is fired, our error variable is written to
directly. This is why in the next line we check to see whether the error is still equal to nil. If it is, we
know our fetch request executed properly, so we simply assign our results array to our class property
closeFriends . If we did get an error, we use NSLog to print it out so that we can debug it later.
Now we need to add our UITableViewDatasource methods.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_closeFriends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath
{
static NSString *CellIdentifier = @"CloseFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
[self configureCloseFriendCell:cell withFriend:_closeFriends[indexPath.row]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)
editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self removeCloseFriend:_closeFriends[indexPath.row]];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
 
Search WWH ::




Custom Search