views:

1016

answers:

3

Hi,

I'm trying to use a tableview without using a nib and without using a UITableViewController.

I have added a UITableView instance to a UIViewController Like So

mytable = [[UITableView alloc] initWithFrame:CGRectMake(22, 207, 270, 233)];
[mytable setDelegate:self];
[self.view mytable];

Also I have added the following table view methods to my UIViewController (cut for brevities sake)

-

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

I am getting a warning saying that my UIViewController does not implement UITableView delegate protocol.

Whats the correct way to tell the table view where its delegate methods are?

(This is my first attempt at trying to use a UITableView without selecting the UITableTableview controller from the new file options)

+2  A: 

You need to conform your class to the UITableViewDelegate and UITableViewDataSource protocols. ( cellForRowAtIndexPath: is in the UITableViewDataSource protocol )

Do this by using angle brackets in your class interface definition:

@interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {...}

The reason why you are getting this warning now and not before when you were using a UITableViewController is because UITableViewController already conforms to these protocols.

So, in essence a UITableViewController is just a class that conforms to UITableViewDelegate and UITableViewDataSource, and has a UITableView instance member. That's pretty much it.

Jacob Relkin
o.k thanks guys, that made the error go away. I guess using <..> is when you want to implement more than one interface? I still have a problem, none of my tableview methods are getting called. [table reloadData] has no effect and also my didSelectRowAtIndexPath method is not executing when I click a cell. Can you think of anything else I might be missing?
dubbeat
Never mind, I forgot to also set datasource aswell as the delegate
dubbeat
A: 

The warning is just telling you that your @interface section should declare that you implement the UITableViewDelegate protocol:

@interface MyUIViewController : UIViewController < UITableViewDelegate >

David Gelhar
+1  A: 

You also must set the dataSource delegate to self:

[tableView setDelegate:self];
[tableView setDataSource:self];

or, equally:

tableview.delegate = self;
tableview.dataSource = self;
MattDiPasquale