Permalink
Browse files

Update RKTwitter Example:

Hook up table view
Spruce Up UI
Get rid of nibs.
  • Loading branch information...
1 parent 5cc67c5 commit 5dc77ed536e68350053b42bb46f3c752d9a82c54 @jeremyellison jeremyellison committed Oct 5, 2010
@@ -8,15 +8,9 @@
#import <UIKit/UIKit.h>
-@class RKTwitterViewController;
@interface RKTwitterAppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
- RKTwitterViewController *viewController;
}
-@property (nonatomic, retain) IBOutlet UIWindow *window;
-@property (nonatomic, retain) IBOutlet RKTwitterViewController *viewController;
-
@end
@@ -14,10 +14,6 @@
@implementation RKTwitterAppDelegate
-@synthesize window;
-@synthesize viewController;
-
-
#pragma mark -
#pragma mark Application lifecycle
@@ -31,16 +27,17 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[mapper registerClass:[RKTUser class] forElementNamed:@"user"];
[mapper registerClass:[RKTStatus class] forElementNamed:@"status"];
- // Add the view controller's view to the window and display.
- [window addSubview:viewController.view];
+ // Create Window and View Controllers
+ RKTwitterViewController* viewController = [[[RKTwitterViewController alloc] initWithNibName:nil bundle:nil] autorelease];
+ UINavigationController* controller = [[UINavigationController alloc] initWithRootViewController:viewController];
+ UIWindow* window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
+ [window addSubview:controller.view];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
- [viewController release];
- [window release];
[super dealloc];
}
@@ -9,14 +9,9 @@
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
-@interface RKTwitterViewController : UIViewController <UITableViewDelegate, RKObjectLoaderDelegate> {
- IBOutlet UITableView* _tableView;
+@interface RKTwitterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate> {
+ UITableView* _tableView;
NSArray* _statuses;
}
-/**
- * An array of RKTStatus objects
- */
-@property (nonatomic, retain) NSArray* statuses;
-
@end
@@ -11,43 +11,83 @@
@implementation RKTwitterViewController
-@synthesize statuses = _statuses;
-
-- (void)viewDidLoad {
- [super viewDidLoad];
+- (void)loadView {
+ [super loadView];
+
+ // Setup View and Table View
self.title = @"Two Toasters Tweets";
+ [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
+ [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
+
+ UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BG.png"]] autorelease];
+ imageView.frame = CGRectOffset(imageView.frame, 0, -64);
+
+ [self.view insertSubview:imageView atIndex:0];
+
+ _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStylePlain];
+ _tableView.dataSource = self;
+ _tableView.delegate = self;
+ [self.view addSubview:_tableView];
+
+ _tableView.backgroundColor = [UIColor clearColor];
+ _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// Load the object model via RestKit
+ // Retain the object loader that is returned so that it is not dealloc'd after the request is sent.
RKObjectManager* objectManager = [RKObjectManager globalManager];
- [objectManager loadObjectsAtResourcePath:@"/status/user_timeline/twotoasters.json" objectClass:[RKTStatus class] delegate:self];
+ [[objectManager loadObjectsAtResourcePath:@"/status/user_timeline/twotoasters.json" objectClass:[RKTStatus class] delegate:self] retain];
}
- (void)dealloc {
+ [_tableView release];
[_statuses release];
[super dealloc];
}
#pragma mark RKObjectLoaderDelegate methods
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
- // TODO: Update the table appropriately...
NSLog(@"Loaded statuses: %@", objects);
- self.statuses = objects;
+ [_statuses release];
+ _statuses = [objects retain];
+ [_tableView reloadData];
+ [objectLoader release];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
- // TODO: Show an error alert
- NSLog(@"Hit error: %@", [error localizedDescription]);
+ UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
+ [alert show];
+ NSLog(@"Hit error: %@", error);
+ [objectLoader release];
}
#pragma mark UITableViewDelegate methods
+- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
+ CGSize size = [[[_statuses objectAtIndex:indexPath.row] text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, 9000)];
+ return size.height + 10;
+}
+
#pragma mark UITableViewDataSource methods
+- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
+ return [_statuses count];
+}
+
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- return nil;
+ NSString* reuseIdentifier = @"Tweet Cell";
+ UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
+ if (nil == cell) {
+ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
+ cell.textLabel.font = [UIFont systemFontOfSize:14];
+ cell.textLabel.numberOfLines = 0;
+ cell.textLabel.backgroundColor = [UIColor clearColor];
+ cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"listbg.png"]];
+ }
+ cell.textLabel.text = [[_statuses objectAtIndex:indexPath.row] text];
+ return cell;
}
@end
Oops, something went wrong.

0 comments on commit 5dc77ed

Please sign in to comment.