Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set a tableview delegate

I'm trying to use a UITableView 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)

like image 291
dubbeat Avatar asked Mar 16 '10 13:03

dubbeat


People also ask

What is tableView delegate?

func tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath) Tells the delegate the table view is about to draw a cell for a particular row. func tableView(UITableView, indentationLevelForRowAt: IndexPath) -> Int. Asks the delegate to return the level of indentation for a row in a given section.

What is tableView delegate and DataSource?

Datasource methods are used to generate tableView cells,header and footer before they are displaying.. Delegate methods provide information about these cells, header and footer along with other user action handlers like cell selection and edit..

What is difference between delegate and DataSource in iOS?

A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data.


1 Answers

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.

Of course, if you're not already subclassing UITableViewController, then you need to manually setup the dataSource and delegate of the UITableView:

[tableView setDelegate:self];
[tableView setDataSource:self];
like image 192
Jacob Relkin Avatar answered Oct 14 '22 02:10

Jacob Relkin