سؤال

I'm learning objective-c and I found the following code in my book. I've got 3 questions,

  1. Obviously it conforms the protocol by implementing two required methods, but why this works without writing <UITableViewDataSource,UITableViewDelegate> in the header?
  2. Which protocol it conforms, UITableViewDataSource or UITableViewDelegate?
  3. Why there is no UITableView.delegate = self?

Here is the code,

@implementation ItemsViewController

-(instancetype) init
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        for (int i = 0; i < 5; i++)
        {
            [[ItemStore sharedStore] creatItem];
        }
    }
    return self;
}
-(instancetype) initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[ItemStore sharedStore] allItems] count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *c = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    NSArray *items = [[ItemStore sharedStore] allItems];
    Item *item = [items objectAtIndex:indexPath.row];
    c.textLabel.text = [item description];
    return c;
}

-(void) viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

@end

Appreciate your help in understanding this.

هل كانت مفيدة؟

المحلول 2

ItemsViewController looks to be a UITableViewController. A UITableViewController instance is the default delegate and datasource for the contained UITableView accessed via self.tableView.

Hence there is no explicit marking of protocol implementation <UITableViewDataSource,UITableViewDelegate> and set self.tableView.delegate = self and self.tableView.datasource = self

You can however, choose to set different instance as delegate and datasource in that case you need to create that instance, implement required methods in it and assign it to the tableView.

Hope that helps!

نصائح أخرى

1. Obviously it conforms the protocol by implementing two required methods, but why this works without writing '<'UITableViewDataSource,UITableViewDelegate'>' in the header?

The '<'UITableViewDataSource,UITableViewDelegate'>' in your header is just an indication to the compiler that you want to implement the delegate methods in your class. You will get warnings if you don't implement delegate methods that are marked as @required, but since most of the delegate methods are usually @optional your code will compile and run fine. That doesn't mean that you shouldn't add the delegates in your header though.

2. Which protocol it conforms, UITableViewDataSource or UITableViewDelegate?

Default it needs only UITableViewDataSource with these two functions must be defined

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


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

3. Why there is no UITableView.delegate = self?

It is there, check your xib you set delegate from the xib too. Right click the UITableView you will understand what I mean, without setting delegate the above methods won't work.

Hope this helps.

I agree with @iphonic but for the first point.

The '<'UITableViewDataSource,UITableViewDelegate'>' in your header is an indication for the compiler that the instances of the class does respond to that protocols. That info is used by compiler as added info for type matching. For example, when you set delegate or dataSource properties for a table view.

If you don't write '<'UITableViewDataSource,UITableViewDelegate'>' in header of the class, and you write something like "tableView.dataSource = self", then you will get warnings. When you use IB or Storyboard you don't write that setting line. That's the reason you don't have to include the protocols declaration in class interface.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top