Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keicoder/9543581 to your computer and use it in GitHub Desktop.
Save keicoder/9543581 to your computer and use it in GitHub Desktop.
objective-c : TableViews Getting Started (Ray Wenderlich) TableViews Chapter 2 Multiple Sections
//TableViews Getting Started (Ray Wenderlich) TableViews Chapter 2 Multiple Sections
//1. Scary Bugs
//ScaryBug.h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, ScaryFactor) {
ScaryFactorNotScary,
ScaryFactorALittleScary,
ScaryFactorAverageScary,
ScaryFactorQuiteScary,
ScaryFactorAiiiiieeeee
};
@interface ScaryBug : NSObject
@property (strong) NSString * name;
@property (retain) UIImage * image;
@property (assign) ScaryFactor howScary;
- (id)initWithName:(NSString *)name image:(UIImage *)image howScary:(ScaryFactor)howScary;
- (NSString *)howScaryString;
+ (NSMutableArray *)bugs;
+ (NSString *)scaryFactorToString:(ScaryFactor)scaryFactor;
@end
//ScaryBug.m
@implementation ScaryBug
- (id)initWithName:(NSString *)name image:(UIImage *)image howScary:(ScaryFactor)howScary {
if ((self = [super init])) {
_name = name;
_image = image;
_howScary = howScary;
}
return self;
}
- (NSString *)howScaryString {
return [ScaryBug scaryFactorToString:self.howScary];
}
+ (NSString *)scaryFactorToString:(ScaryFactor)scaryFactor {
switch (scaryFactor) {
case ScaryFactorNotScary:
return @"Not scary";
break;
case ScaryFactorALittleScary:
return @"A little scary";
break;
case ScaryFactorAverageScary:
return @"Average scariness";
break;
case ScaryFactorQuiteScary:
return @"Quite scary";
break;
case ScaryFactorAiiiiieeeee:
return @"AIIIIIEEEEE!!";
break;
}
}
+ (NSMutableArray *)bugs {
ScaryBug * centipede = [[ScaryBug alloc] initWithName:@"Centipede" image:[UIImage imageNamed:@"centipede.jpg"] howScary:ScaryFactorAverageScary];
ScaryBug * ladybug = [[ScaryBug alloc] initWithName:@"Ladybug" image:[UIImage imageNamed:@"ladybug.jpg"] howScary:ScaryFactorNotScary];
ScaryBug * potatoBug = [[ScaryBug alloc] initWithName:@"Potato Bug" image:[UIImage imageNamed:@"potatoBug.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * wolfSpider = [[ScaryBug alloc] initWithName:@"Wolf Spider" image:[UIImage imageNamed:@"wolfSpider.jpg"] howScary:ScaryFactorAiiiiieeeee];
ScaryBug * bee = [[ScaryBug alloc] initWithName:@"Bee" image:[UIImage imageNamed:@"bee.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * beetle = [[ScaryBug alloc] initWithName:@"Beetle" image:[UIImage imageNamed:@"beetle.jpg"] howScary:ScaryFactorALittleScary];
ScaryBug * burritoInsect = [[ScaryBug alloc] initWithName:@"Burrito Insect" image:[UIImage imageNamed:@"burritoInsect.jpg"] howScary:ScaryFactorAverageScary];
ScaryBug * caterpillar = [[ScaryBug alloc] initWithName:@"Caterpillar" image:[UIImage imageNamed:@"caterpillar.jpg"] howScary:ScaryFactorNotScary];
ScaryBug * cicada = [[ScaryBug alloc] initWithName:@"Cicada" image:[UIImage imageNamed:@"cicada.jpg"] howScary:ScaryFactorAverageScary];
ScaryBug * cockroach = [[ScaryBug alloc] initWithName:@"Cockroach" image:[UIImage imageNamed:@"cockroach.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * exoskeleton = [[ScaryBug alloc] initWithName:@"Exoskeleton" image:[UIImage imageNamed:@"exoskeleton.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * fly = [[ScaryBug alloc] initWithName:@"Fly" image:[UIImage imageNamed:@"fly.jpg"] howScary:ScaryFactorNotScary];
ScaryBug * giantMoth = [[ScaryBug alloc] initWithName:@"Giant Moth" image:[UIImage imageNamed:@"wolfSpider.jpg"] howScary:ScaryFactorAverageScary];
ScaryBug * grasshopper = [[ScaryBug alloc] initWithName:@"Grasshopper" image:[UIImage imageNamed:@"grasshopper.jpg"] howScary:ScaryFactorAiiiiieeeee];
ScaryBug * mosquito = [[ScaryBug alloc] initWithName:@"Mosquito" image:[UIImage imageNamed:@"mosquito.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * prayingMantis = [[ScaryBug alloc] initWithName:@"Praying Mantis" image:[UIImage imageNamed:@"prayingMantis.jpg"] howScary:ScaryFactorNotScary];
ScaryBug * roach = [[ScaryBug alloc] initWithName:@"Roach" image:[UIImage imageNamed:@"roach.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * robberFly = [[ScaryBug alloc] initWithName:@"Robber Fly" image:[UIImage imageNamed:@"robberFly.jpg"] howScary:ScaryFactorQuiteScary];
ScaryBug * scorpion = [[ScaryBug alloc] initWithName:@"Scorpion" image:[UIImage imageNamed:@"scorpion.jpg"] howScary:ScaryFactorAiiiiieeeee];
ScaryBug * shieldBug = [[ScaryBug alloc] initWithName:@"Shield Bug" image:[UIImage imageNamed:@"shieldBug.jpg"] howScary:ScaryFactorAverageScary];
ScaryBug * stagBeetle = [[ScaryBug alloc] initWithName:@"Stag Beetle" image:[UIImage imageNamed:@"stagBeetle.jpg"] howScary:ScaryFactorAverageScary];
ScaryBug * stinkBug = [[ScaryBug alloc] initWithName:@"Stink Bug" image:[UIImage imageNamed:@"stinkbug.jpg"] howScary:ScaryFactorALittleScary];
return [@[centipede, ladybug, potatoBug, wolfSpider, bee, beetle, burritoInsect, caterpillar, cicada, cockroach, exoskeleton, fly, giantMoth, grasshopper, mosquito, prayingMantis, roach, robberFly, scorpion, shieldBug, stagBeetle, stinkBug] mutableCopy];
}
@end
//AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//AppDelegate.m
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
@end
//BugTableViewController.h
@interface BugTableViewController : UITableViewController
@end
//BugTableViewController.m
#import "ScaryBug.h"
@interface BugTableViewController ()
@property (strong, nonatomic) NSMutableArray *bugs;
@end
@implementation BugTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.bugs = [ScaryBug bugs];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.bugs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BugCell" forIndexPath:indexPath];
ScaryBug *bug = self.bugs[indexPath.row];
cell.textLabel.text = bug.name;
cell.detailTextLabel.text = bug.howScaryString;
cell.imageView.image = bug.image;
return cell;
}
@end
//2. Multiple Sections
//BugSection.h
#import "ScaryBug.h"
@interface BugSection : NSObject
@property (assign) ScaryFactor howScary;
@property (strong, nonatomic) NSMutableArray *bugs;
- (instancetype)initWithHowScary:(ScaryFactor)howScary;
@end
//BugSection.m
@implementation BugSection
- (instancetype)initWithHowScary:(ScaryFactor)howScary
{
if ((self = [super init]))
{
_howScary = howScary;
_bugs = [NSMutableArray array];
}
return self;
}
@end
//BugTableViewController.h
@interface BugTableViewController : UITableViewController
@end
//BugTableViewController.m
#import "ScaryBug.h"
#import "BugSection.h"
@interface BugTableViewController ()
@property (strong, nonatomic) NSMutableArray *bugSections;
@end
@implementation BugTableViewController
//sort the bugs into sections
- (void)setupBugs {
// Create sections
self.bugSections = [NSMutableArray arrayWithCapacity:5];
[self.bugSections addObject:[[BugSection alloc] initWithHowScary:ScaryFactorNotScary]];
[self.bugSections addObject:[[BugSection alloc] initWithHowScary:ScaryFactorALittleScary]];
[self.bugSections addObject:[[BugSection alloc] initWithHowScary:ScaryFactorAverageScary]];
[self.bugSections addObject:[[BugSection alloc] initWithHowScary:ScaryFactorQuiteScary]];
[self.bugSections addObject:[[BugSection alloc] initWithHowScary:ScaryFactorAiiiiieeeee]];
// Sort bugs into sections
NSMutableArray *bugs = [ScaryBug bugs];
for (ScaryBug *bug in bugs) {
BugSection *section = self.bugSections[(int) bug.howScary];
[section.bugs addObject:bug];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupBugs];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.bugSections.count;
}
//if you want a custom look for the header, implement tableView:viewForHeaderInSection: instead
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
BugSection *bugSection = self.bugSections[section];
return [ScaryBug scaryFactorToString:bugSection.howScary];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
BugSection *bugSection = self.bugSections[section];
return bugSection.bugs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BugCell" forIndexPath:indexPath];
BugSection *bugSection = self.bugSections[indexPath.section];
ScaryBug *bug = bugSection.bugs[indexPath.row];
cell.textLabel.text = bug.name;
cell.detailTextLabel.text = bug.howScaryString;
cell.imageView.image = bug.image;
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment