Blended Cocoa

Adventures in Objective-C, Swift and Cocoa

Dress Classy, Dance Cheesy!

I’m sure you’ve always wanted to be able to create a UITableView with the following code:

UITableView initWithFrame:… style:…
1
2
UITableView *tableView = [[UITableView alloc] initWithFrame:frame
                                                      style:UITableViewStyleGangnam];

UITableViewStyleGangnam

I was inspired to write the following after my trip to /dev/world last month. Gangnam Style was one of the memes during the conference. Feel free to do something even more cheesy with it.

The following source code and an example iPhone project are available on GitHub at: https://github.com/mttrb/UITableView-GangnamStyle

UITableView+GangnamStyle.hView GitHub
1
2
3
4
5
6
7
8
9
10
11
//
//  UITableView+GangnamStyle.h
//  Gangnam Style
//
//  Created by Matthew Robinson on 20th October 2012.
//  Copyright (c) 2012 Matthew Robinson. All rights reserved.
//

#import <UIKit/UIKit.h>

#define UITableViewStyleGangnam ((UITableViewStyle)1000)
UITableView+GangnamStyle.mView GitHub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
//  UITableView+GangnamStyle.m
//  Gangnam Style
//
//  Created by Matthew Robinson on 20/10/12.
//  Copyright (c) 2012 Matthew Robinson. All rights reserved.
//

#import "UITableView+GangnamStyle.h"
#import <objc/runtime.h>


@interface PSYGangnam : NSObject <UITableViewDataSource>

+ (id)sharedGangnam;

@end


@implementation UITableView (GangnamStyle)

+ (void)load {
    method_exchangeImplementations(
        class_getInstanceMethod(self, @selector(initWithFrame:style:)),
        class_getInstanceMethod(self, @selector(PSY_initWithFrame:style:))
    );
}

- (id)PSY_initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    // Call the original initWithFrame:style: which is now
    // called PSY_initWithFrame:style: This is NOT recursive!
    // If the style is Gangnam actually use plain

    self = [self PSY_initWithFrame:frame
                             style:style == UITableViewStyleGangnam ? UITableViewStylePlain : style];


    // Set the data source if Gangnam style
    if (self && style == UITableViewStyleGangnam) {
        [self setDataSource:[PSYGangnam sharedGangnam]];
    }

    return self;
}

@end



@interface PSYGangnam () {
    NSArray *_lyrics;
}

@end

@implementation PSYGangnam

+ (id)sharedGangnam {
    static PSYGangnam *sharedInstance = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        sharedInstance = [[PSYGangnam alloc] init];
    });

    return sharedInstance;
}


- (id)init {
    self = [super init];

    if (self) {
        _lyrics = [[NSArray alloc] initWithObjects:
                   @"Oppan Gangnam Style",
                   @"Gangnam Style",
                   @"",
                   @"Na je nun ta sa ro un in gan jo gin yo ja",

                   // ...

                   @"Oppan Gangnam Style",
                   @"",
                   @"© Sony/ATV Music Publishing LLC",
                   nil
                   ];
    }

    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_lyrics count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *PSYGangnamCellIdentifier = @"PSYGangnamCellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PSYGangnamCellIdentifier];

    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:PSYGangnamCellIdentifier] autorelease];

        [[cell textLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:11.0]];
    }

    [[cell textLabel] setText:[_lyrics objectAtIndex:[indexPath row]]];

    return cell;
}

@end

Comments