Skip to content

Commit

Permalink
add fetched results controller
Browse files Browse the repository at this point in the history
  • Loading branch information
100mango committed Apr 8, 2015
1 parent a818920 commit 0f1c7d9
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 6 deletions.
27 changes: 23 additions & 4 deletions QRCatcher/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6736"/>
</dependencies>
<scenes>
<!--Item 2-->
<!--URL-->
<scene sceneID="S0d-K9-LNM">
<objects>
<viewController id="tLp-OF-DMW" customClass="QRURLViewController" sceneMemberID="viewController">
<viewController title="URL" id="tLp-OF-DMW" customClass="QRURLViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="aVG-X8-ap0"/>
<viewControllerLayoutGuide type="bottom" id="1jC-g8-Vry"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="3EE-mB-aNd">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="ObY-vt-qiC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<connections>
<outlet property="dataSource" destination="tLp-OF-DMW" id="94c-DG-9PU"/>
<outlet property="delegate" destination="tLp-OF-DMW" id="INH-Fw-Yns"/>
</connections>
</tableView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="ObY-vt-qiC" secondAttribute="bottom" id="5a7-lz-O6y"/>
<constraint firstAttribute="trailing" secondItem="ObY-vt-qiC" secondAttribute="trailing" id="76C-hM-o4V"/>
<constraint firstItem="ObY-vt-qiC" firstAttribute="top" secondItem="3EE-mB-aNd" secondAttribute="top" id="NHx-RI-Jkd"/>
<constraint firstItem="ObY-vt-qiC" firstAttribute="leading" secondItem="3EE-mB-aNd" secondAttribute="leading" id="RPL-sC-aH8"/>
</constraints>
</view>
<tabBarItem key="tabBarItem" title="Item 2" id="Nhe-Mq-UOZ"/>
<connections>
<outlet property="tableView" destination="ObY-vt-qiC" id="bma-3G-Xq5"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="7TF-Vs-bMd" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
Expand All @@ -42,10 +61,10 @@
</objects>
<point key="canvasLocation" x="-720" y="-183"/>
</scene>
<!--Item-->
<!--QRView-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="QRCatchViewController" sceneMemberID="viewController">
<viewController title="QRView" id="vXZ-lx-hvc" customClass="QRCatchViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
Expand Down
95 changes: 93 additions & 2 deletions QRCatcher/QRURLViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,114 @@
//

#import "QRURLViewController.h"
#import "AppDelegate.h"

@interface QRURLViewController ()
@interface QRURLViewController ()<UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate>

@property (nonatomic,strong) NSFetchedResultsController *fetchedResultsController;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation QRURLViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.


//fetched results controller set up
NSFetchRequest *requst = [NSFetchRequest fetchRequestWithEntityName:@"URLEntity"];
NSManagedObjectContext *cotext = [[AppDelegate appDelegate] managedObjectContext];

self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:requst managedObjectContext:cotext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController.delegate = self;
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];

if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.fetchedResultsController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.fetchedResultsController.sections.count > 0)
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
else
return 0;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = <#Get the cell#>;
//NSManagedObject *managedObject = [<#Fetched results controller#> objectAtIndexPath:indexPath];
// Configure the cell with data from the managed object.
//return cell;
}
*/

#pragma mark - Table view delegate

#pragma mark - NSFetchedResultsController delegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {

UITableView *tableView = self.tableView;

switch(type) {

case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;

case NSFetchedResultsChangeUpdate:
//[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
//atIndexPath:indexPath];
break;

case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

/*
#pragma mark - Navigation
Expand Down

0 comments on commit 0f1c7d9

Please sign in to comment.