0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
john cs
  • 2,220
  • 5
  • 32
  • 50
  • I don't see anywhere in your code where you are setting voteTable's frame or adding it to your view. – Jsdodgers Aug 24 '13 at 23:34
  • A table view should not be its own delegate, as this goes against MVC principles. You should have a UIViewController subclass or a UITableViewController subclass as the delegate. – rdelmar Aug 25 '13 at 00:18
  • @rdelmar - Yet this is a very common pattern in Objective-C, and the world doesn't fall apart as a result. – Hot Licks Aug 25 '13 at 01:19
  • @HotLicks, I meant to say data source not delegate in my comment. That certainly would be bad MVC practice to have a table view be its own data source. – rdelmar Aug 25 '13 at 01:32
  • @rdelmar - Why? Just because it "violates MVC"? Heck, no one really pays attention to MVC, even though many give it lip service. – Hot Licks Aug 25 '13 at 02:30

1 Answers1

1

What is vTable? Looks like you have a "voteTable" class (BTW: classes should start with an uppercase char, instance variables should start with lowercase). Anyhow, looks like your main problem is you forgot to add the table as a subview and set its frame. eg:

self.voteTbl = [[VoteTable alloc] init];
self.voteTbl.delegate = self;
self.voteTbl.dataSource = self;
self.voteTbl.frame = self.view.bounds;
[self.view addSubView:self.voteTbl];
Nicholas Hart
  • 1,734
  • 13
  • 22
  • Thank you! I didn't realize how stepping away from IB would be so different....using this method do I lose all control from IB (drag and drop positioning)? Is there any way to link this to the table I made in IB? They have the same reference outlet. – john cs Aug 24 '13 at 23:39
  • You still have full control--you need to set the frame so that it shows up in the position/size you want. You can also use autolayout constraints programatically. eg: http://stackoverflow.com/questions/15893022/setting-constraints-programatically – Nicholas Hart Aug 24 '13 at 23:56
  • 1
    @johnhannigan - You can, in IB, link to your VoteTable class. IIRC (I have to puzzle over it every time I do it) you select a TableView in IB but change it's class to VoteTable in the tabs on the right. (You, of course, still have to set delegate and dataSource yourself.) – Hot Licks Aug 25 '13 at 01:22