Saturday, 24 August 2013

Set delegate to an object

Set delegate to an object

This is the first time im trying to set a UITableView delegate/datasource
to an instance of an object.
I have a UIView managed by a class called hMain, and a UITableView inside
of main managed by an instance of a class called vTable.
hMain.h:
@interface hMain : UIViewController
@property (strong, nonatomic) IBOutlet vTable *voteTbl;
@end
hMain.m:
- (void)viewDidLoad
{
[super viewDidLoad];
voteTbl = [[vTable alloc]init];
[self.voteTbl setDelegate:voteTbl];
[self.voteTbl setDataSource:voteTbl];
}
vTable.h:
@interface vTable : UITableView <UITableViewDelegate , UITableViewDataSource>
@end
vTable.M:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [UITableViewCell
configureFlatCellWithColor:[UIColor greenSeaColor]
selectedColor:[UIColor wetAsphaltColor] reuseIdentifier:CellIdentifier
inTableView:(UITableView *)tableView];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
[cell configureFlatCellWithColor:[UIColor greenSeaColor]
selectedColor:[UIColor wetAsphaltColor]];
}
cell.textLabel.text = @"Hello there!";
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Row pressed!!");
}
This is really my first time straying away from IB and doing things
programatically so im not sure im settings delegates and things correctly.
This is also my first attempt and setting a delegate/datasource outside of
self.
My problem is the table is coming out blank every time.

No comments:

Post a Comment