Skip to content

Instantly share code, notes, and snippets.

@winder
Created October 9, 2010 03:34
Show Gist options
  • Save winder/617846 to your computer and use it in GitHub Desktop.
Save winder/617846 to your computer and use it in GitHub Desktop.
...
// 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];
...
#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
#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