Skip to content

Commit

Permalink
New example added, demonstrating the use of EasyTableView with public…
Browse files Browse the repository at this point in the history
… images remotely loaded from Flickr.
  • Loading branch information
Aleksey Novicov committed Jan 31, 2012
1 parent 27a8852 commit dc67a7b
Show file tree
Hide file tree
Showing 12 changed files with 805 additions and 513 deletions.
2 changes: 1 addition & 1 deletion Classes/EasyTableAppDelegate.m
Expand Up @@ -21,7 +21,7 @@ @implementation EasyTableAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


// Override point for customization after app launch. // Override point for customization after app launch.
[window addSubview:viewController.view]; window.rootViewController = viewController;
[window makeKeyAndVisible]; [window makeKeyAndVisible];


return YES; return YES;
Expand Down
3 changes: 2 additions & 1 deletion Classes/EasyTableViewController.h
Expand Up @@ -8,8 +8,9 @@


#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "EasyTableView.h" #import "EasyTableView.h"
#import "FlipsideViewController.h"


@interface EasyTableViewController : UIViewController <EasyTableViewDelegate> { @interface EasyTableViewController : UIViewController <EasyTableViewDelegate, FlipsideViewControllerDelegate> {
IBOutlet UILabel *bigLabel; IBOutlet UILabel *bigLabel;
EasyTableView *verticalView; EasyTableView *verticalView;
EasyTableView *horizontalView; EasyTableView *horizontalView;
Expand Down
23 changes: 18 additions & 5 deletions Classes/EasyTableViewController.m
Expand Up @@ -64,7 +64,8 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface


- (void)setupHorizontalView { - (void)setupHorizontalView {
CGRect frameRect = CGRectMake(0, LANDSCAPE_HEIGHT - HORIZONTAL_TABLEVIEW_HEIGHT, PORTRAIT_WIDTH, HORIZONTAL_TABLEVIEW_HEIGHT); CGRect frameRect = CGRectMake(0, LANDSCAPE_HEIGHT - HORIZONTAL_TABLEVIEW_HEIGHT, PORTRAIT_WIDTH, HORIZONTAL_TABLEVIEW_HEIGHT);
self.horizontalView = [[EasyTableView alloc] initWithFrame:frameRect numberOfColumns:NUM_OF_CELLS ofWidth:VERTICAL_TABLEVIEW_WIDTH]; EasyTableView *view = [[EasyTableView alloc] initWithFrame:frameRect numberOfColumns:NUM_OF_CELLS ofWidth:VERTICAL_TABLEVIEW_WIDTH];
self.horizontalView = view;


horizontalView.delegate = self; horizontalView.delegate = self;
horizontalView.tableView.backgroundColor = TABLE_BACKGROUND_COLOR; horizontalView.tableView.backgroundColor = TABLE_BACKGROUND_COLOR;
Expand All @@ -74,13 +75,14 @@ - (void)setupHorizontalView {
horizontalView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth; horizontalView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;


[self.view addSubview:horizontalView]; [self.view addSubview:horizontalView];
[horizontalView release]; [view release];
} }




- (void)setupVerticalView { - (void)setupVerticalView {
CGRect frameRect = CGRectMake(PORTRAIT_WIDTH - VERTICAL_TABLEVIEW_WIDTH, 0, VERTICAL_TABLEVIEW_WIDTH, LANDSCAPE_HEIGHT); CGRect frameRect = CGRectMake(PORTRAIT_WIDTH - VERTICAL_TABLEVIEW_WIDTH, 0, VERTICAL_TABLEVIEW_WIDTH, LANDSCAPE_HEIGHT);
self.verticalView = [[EasyTableView alloc] initWithFrame:frameRect numberOfRows:NUM_OF_CELLS ofHeight:HORIZONTAL_TABLEVIEW_HEIGHT]; EasyTableView *view = [[EasyTableView alloc] initWithFrame:frameRect numberOfRows:NUM_OF_CELLS ofHeight:HORIZONTAL_TABLEVIEW_HEIGHT];
self.verticalView = view;


verticalView.delegate = self; verticalView.delegate = self;
verticalView.tableView.backgroundColor = TABLE_BACKGROUND_COLOR; verticalView.tableView.backgroundColor = TABLE_BACKGROUND_COLOR;
Expand All @@ -93,7 +95,7 @@ - (void)setupVerticalView {
verticalView.tableView.contentInset = UIEdgeInsetsMake(0, 0, HORIZONTAL_TABLEVIEW_HEIGHT, 0); verticalView.tableView.contentInset = UIEdgeInsetsMake(0, 0, HORIZONTAL_TABLEVIEW_HEIGHT, 0);


[self.view addSubview:verticalView]; [self.view addSubview:verticalView];
[verticalView release]; [view release];
} }




Expand All @@ -107,7 +109,6 @@ - (void)borderIsSelected:(BOOL)selected forView:(UIView *)view {
} }





#pragma mark - #pragma mark -
#pragma mark EasyTableViewDelegate #pragma mark EasyTableViewDelegate


Expand Down Expand Up @@ -159,5 +160,17 @@ - (void)easyTableView:(EasyTableView *)easyTableView selectedView:(UIView *)sele
bigLabel.text = label.text; bigLabel.text = label.text;
} }


#pragma mark - Flipside View Controller

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil] autorelease];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}


@end @end
29 changes: 29 additions & 0 deletions Classes/FlipsideViewController.h
@@ -0,0 +1,29 @@
//
// FlipsideViewController.h
// EasyTableView
//
// Created by Aleksey Novicov on 1/30/12.
// Copyright (c) 2012 Yodel Code LLC. All rights reserved.
//
// Demonstrates the use of EasyTableView with images loaded from a remote server

#import <UIKit/UIKit.h>
#import "EasyTableView.h"
#import "ImageStore.h"

@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

@interface FlipsideViewController : UIViewController <EasyTableViewDelegate, ImageStoreDelegate>

@property (assign, nonatomic) IBOutlet id<FlipsideViewControllerDelegate> delegate;
@property (retain, nonatomic) IBOutlet UILabel *errorLabel;
@property (retain, nonatomic) ImageStore *imageStore;
@property (retain, nonatomic) EasyTableView *easyTableView;

- (IBAction)done:(id)sender;

@end
153 changes: 153 additions & 0 deletions Classes/FlipsideViewController.m
@@ -0,0 +1,153 @@
//
// FlipsideViewController.m
// EasyTableView
//
// Created by Aleksey Novicov on 1/30/12.
// Copyright (c) 2012 Yodel Code LLC. All rights reserved.
//
// Demonstrates the use of EasyTableView with images loaded from a remote server

#import "FlipsideViewController.h"

#define TABLEVIEW_HEIGHT 140
#define TABLECELL_WIDTH 180

#define LABEL_TAG 100
#define IMAGE_TAG 101

@implementation FlipsideViewController

@synthesize delegate = _delegate;
@synthesize imageStore = _imageStore;
@synthesize easyTableView = _easyTableView;
@synthesize errorLabel = _errorLabel;

- (void)dealloc {
[_imageStore release];
[_easyTableView release];
[_errorLabel release];

[super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
ImageStore *store = [[ImageStore alloc] initWithDelegate:self];

self.imageStore = store;
[store release];
}
return self;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

[self.imageStore clearImageCache];
}

#pragma mark - View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload {
[super viewDidUnload];

self.errorLabel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

#pragma mark -
#pragma mark EasyTableView Initialization

- (void)setupEasyTableViewWithNumCells:(NSUInteger)count {
CGRect frameRect = CGRectMake(0, 44, self.view.bounds.size.width, TABLEVIEW_HEIGHT);
EasyTableView *view = [[EasyTableView alloc] initWithFrame:frameRect numberOfColumns:count ofWidth:TABLECELL_WIDTH];
self.easyTableView = view;

self.easyTableView.delegate = self;
self.easyTableView.tableView.backgroundColor = [UIColor clearColor];
self.easyTableView.tableView.separatorColor = [UIColor blackColor];
self.easyTableView.cellBackgroundColor = [UIColor blackColor];
self.easyTableView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;

[self.view addSubview:self.easyTableView];
[view release];
}

#pragma mark - EasyTableViewDelegate

- (UIView *)easyTableView:(EasyTableView *)easyTableView viewForRect:(CGRect)rect {
UIView *container = [[[UIView alloc] initWithFrame:rect] autorelease];;

// Setup an image view to display an image
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(1, 0, rect.size.width-2, rect.size.height)];
imageView.tag = IMAGE_TAG;
imageView.contentMode = UIViewContentModeScaleAspectFill;

[container addSubview:imageView];
[imageView release];

// Setup a label to display the image title
CGRect labelRect = CGRectMake(10, rect.size.height-20, rect.size.width-20, 20);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
label.tag = LABEL_TAG;

[container addSubview:label];
[label release];

return container;
}

// Second delegate populates the views with data from a data source

- (void)easyTableView:(EasyTableView *)easyTableView setDataForView:(UIView *)view forIndex:(NSUInteger)index {
// Set the image title for the given index
UILabel *label = (UILabel *)[view viewWithTag:LABEL_TAG];
label.text = [self.imageStore.titles objectAtIndex:index];

// Set the image for the given index
UIImageView *imageView = (UIImageView *)[view viewWithTag:IMAGE_TAG];
imageView.image = [self.imageStore imageAtIndex:index];
}

#pragma mark - ImageStoreDelegate

- (void)imageTitles:(NSArray *)titles {
[self setupEasyTableViewWithNumCells:[titles count]];
}

- (void)errorMessage:(NSString *)message {
self.errorLabel.text = message;
}

- (void)image:(UIImage *)image loadedAtIndex:(NSUInteger)index {
UIView *view = [self.easyTableView viewAtIndex:index];

// The view might be nil if the cell has scrolled offscreen while we were waiting for the image to load.
// In that case, there is no need to set the image, nor is it even possible.
if (view) {
// Set the image for the view (cell)
UIImageView *imageView = (UIImageView *)[view viewWithTag:IMAGE_TAG];
imageView.image = image;
}
}

#pragma mark - Actions

- (IBAction)done:(id)sender {
[self.delegate flipsideViewControllerDidFinish:self];
}

@end
30 changes: 30 additions & 0 deletions Classes/ImageStore.h
@@ -0,0 +1,30 @@
//
// ImageStore.h
// EasyTableView
//
// Created by Aleksey Novicov on 1/30/12.
// Copyright (c) 2012 Yodel Code LLC. All rights reserved.
//
// This class loads the most recently added public Flickr images.

#import <Foundation/Foundation.h>

@protocol ImageStoreDelegate <NSObject>
- (void)imageTitles:(NSArray *)titles;
- (void)errorMessage:(NSString *)message;
- (void)image:(UIImage *)image loadedAtIndex:(NSUInteger)index;
@end

@interface ImageStore : NSObject

@property (assign, nonatomic) id<ImageStoreDelegate> delegate;
@property (retain, nonatomic) NSOperationQueue *operationQueue;
@property (retain, nonatomic) NSArray *titles;
@property (retain, nonatomic) NSArray *urls;
@property (retain, nonatomic) NSMutableDictionary *imageCache;

- (id)initWithDelegate:(id<ImageStoreDelegate>)delegate;
- (UIImage *)imageAtIndex:(NSUInteger)index;
- (void)clearImageCache;

@end

0 comments on commit dc67a7b

Please sign in to comment.