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/9542360 to your computer and use it in GitHub Desktop.
Save keicoder/9542360 to your computer and use it in GitHub Desktop.
objective-c :
//TableViews Getting Started (Ray Wenderlich) TableViews Chapter 1 Getting Started
//1. PrettyIcons
//Icon.h
typedef NS_ENUM(NSInteger, RatingType) {
RatingTypeUnrated,
RatingTypeUgly,
RatingTypeOK,
RatingTypeNice,
RatingTypeAwesome
};
@interface Icon : NSObject
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) UIImage *image;
@property (assign) RatingType rating;
- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle imageName:(NSString *)imageName;
@end
//Icon.m
@implementation Icon
- (instancetype)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle imageName:(NSString *)imageName {
if ((self = [super init])) {
_title = title;
_subtitle = subtitle;
_image = [UIImage imageNamed:imageName];
_rating = RatingTypeUnrated;
}
return self;
}
@end
//IconSet.h
@interface IconSet : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSMutableArray *icons;
+ (NSMutableArray *)iconSets;
- (instancetype)initWithName:(NSString *)name icons:(NSMutableArray *)icons;
@end
//IconSet.m
#import "Icon.h"
@implementation IconSet
- (instancetype)initWithName:(NSString *)name icons:(NSMutableArray *)icons {
if ((self = [super init])) {
_name = name;
_icons = icons;
}
return self;
}
+ (IconSet *)winterSet {
NSMutableArray *icons = [NSMutableArray array];
[icons addObject:[[Icon alloc] initWithTitle:@"Ornament" subtitle:@"Hang on your tree" imageName:@"icons_winter_01.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Candy Cane" subtitle:@"Mmm, tasty" imageName:@"icons_winter_02.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Snowman" subtitle:@"A very happy soul" imageName:@"icons_winter_03.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Penguin" subtitle:@"Mario's friend" imageName:@"icons_winter_04.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Santa Hat" subtitle:@"Found it in the chimney" imageName:@"icons_winter_05.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Gift" subtitle:@"Under the tree" imageName:@"icons_winter_06.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Gingerbread Man" subtitle:@"Lives in a yummy house" imageName:@"icons_winter_07.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Christmas Tree" subtitle:@"Smells good" imageName:@"icons_winter_08.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Snowflake" subtitle:@"Unique and beautiful" imageName:@"icons_winter_09.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Reindeer" subtitle:@"A very shiny nose" imageName:@"icons_winter_10.png"]];
return [[IconSet alloc] initWithName:@"Winter" icons:icons];
}
+ (IconSet *)summerSet {
NSMutableArray *icons = [NSMutableArray array];
[icons addObject:[[Icon alloc] initWithTitle:@"Sun" subtitle:@"A beautiful day" imageName:@"summericons_100px_01.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Beach Ball" subtitle:@"Fun in the sand" imageName:@"summericons_100px_02.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Swim Trunks" subtitle:@"Time to go swimming" imageName:@"summericons_100px_03.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Bikini" subtitle:@"Bow-chicka-bow-wow" imageName:@"summericons_100px_04.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Sand Bucket and Shovel" subtitle:@"A castle in the sand" imageName:@"summericons_100px_05.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Surfboard" subtitle:@"Catch a wave" imageName:@"summericons_100px_06.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Strawberry Dacquari" subtitle:@"Great way to relax" imageName:@"summericons_100px_07.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Sunglasses" subtitle:@"I wear them at night" imageName:@"summericons_100px_08.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Flip Flops" subtitle:@"Sand between your toes" imageName:@"summericons_100px_09.png"]];
[icons addObject:[[Icon alloc] initWithTitle:@"Ice Cream" subtitle:@"A summer treat" imageName:@"summericons_100px_10.png"]];
return [[IconSet alloc] initWithName:@"Summer" icons:icons];
}
+ (NSMutableArray *)iconSets {
return [@[[self winterSet], [self summerSet]] 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;
}
//ViewController.h
@interface ViewController : UITableViewController
@end
//ViewController.m
#import "IconSet.h"
#import "Icon.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSArray *icons;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *iconSets = [IconSet iconSets];
IconSet *set = (IconSet *)iconSets[0];
self.icons = set.icons;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.icons.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create the cell (based on prototype)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IconCell" forIndexPath:indexPath];
// Configure the cell
Icon *icon = self.icons[indexPath.row];
cell.textLabel.text = icon.title;
cell.detailTextLabel.text = icon.subtitle;
cell.imageView.image = icon.image;
return cell;
}
@end
//2. Multiple Sections
//Change below code
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
//@property (strong, nonatomic) NSArray *icons; ->
@property (strong, nonatomic) NSArray *iconSets;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.iconSets = [IconSet iconSets];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.iconSets.count;
}
//if you want a custom look for the header, implement tableView:viewForHeaderInSection: instead
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
IconSet *set = self.iconSets[section];
return set.name;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
IconSet *set = self.iconSets[section];
return set.icons.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create the cell (based on prototype)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IconCell" forIndexPath:indexPath];
// Configure the cell
IconSet *set = self.iconSets[indexPath.section];
Icon *icon = set.icons[indexPath.row];
cell.textLabel.text = icon.title;
cell.detailTextLabel.text = icon.subtitle;
cell.imageView.image = icon.image;
return cell;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment