dubbeat
dubbeat

Reputation: 7847

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)

Upvotes: 13

Views: 44455

Answers (5)

Dinesh Reddy Chennuru
Dinesh Reddy Chennuru

Reputation: 491

[tableview setDataSource:self]

You have to give it definitely, Why because of the two required methods of UITableView are under UITableViewDataSource protocol.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Upvotes: 1

silentsudo
silentsudo

Reputation: 6963

In Swift3 this is how you set UITableViewDelegate:

class FeedsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      @IBOutlet weak var feedsTableView: UITableView!
      override func viewDidLoad() {
            super.viewDidLoad()

            feedsTableView.delegate = self
            feedsTableView.dataSource = self

            // Do any additional setup after loading the view.
      }
}

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163238

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];

Upvotes: 31

David Gelhar
David Gelhar

Reputation: 27900

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

@interface MyUIViewController : UIViewController < UITableViewDelegate >

Upvotes: 1

ma11hew28
ma11hew28

Reputation: 126327

You also must set the dataSource delegate to self:

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

or, equally:

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

Upvotes: 18

Related Questions