Created
October 9, 2010 03:34
-
-
Save winder/617846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// To use, place this code somewhere in your View Controller, | |
// all you need it to do is give it a size and an array of | |
// data. | |
NSMutableArray* data = | |
[[NSMutableArray alloc] initWithObjects: | |
@"Andy",@"Erik",@"Aaron",@"Frank",@"Will",@"Joe",nil]; | |
SimpleTableView *simpleTableView = | |
[[SimpleTableView alloc] initWithFrame:self.view.frame | |
andDataArray:data]; | |
[self.view addSubView:simpleTableView]; | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface SimpleTableView : UITableView | |
<UITableViewDelegate, UITableViewDataSource> { | |
NSMutableArray *dataArray; | |
} | |
@property (nonatomic,retain) DataArray *dataArray; | |
// Change this init function to whatever is convenient to you. | |
- (id) initWithFrame:(CGRect)theFrame | |
andDataArray:(NSMutableArray*)data; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SimpleTableView.h" | |
@implementation SimpleTableView | |
@synthesize dataArray; | |
- (id) initWithFrame:(CGRect)theFrame | |
andDataArray:(NSMutableArray*)data { | |
if (self = [super initWithFrame:theFrame]) { | |
// This is the "Trick", set the delegates to self. | |
self.dataArray = data; | |
self.dataSource = self; | |
self.delegate = self; | |
} | |
return self; | |
} | |
#pragma mark - | |
#pragma mark Table View Delegates | |
- (NSInteger) | |
numberOfSectionsInTableView:(UITableView *)tableView { | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView | |
numberOfRowsInSection:(NSInteger)section { | |
return [dataArray count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView | |
cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *CellIdentifier = @"Cell"; | |
UITableViewCell *cell = | |
[tableView | |
dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (cell == nil) { | |
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero | |
reuseIdentifier:CellIdentifier] autorelease]; | |
} | |
cell.textLabel.text = | |
[dataArray objectAtIndex:indexPath.row]; | |
return cell; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment