为什么视图控制器的标头中没有协议声明?

时间:2014-03-19 05:42:01

标签: ios objective-c uitableview delegates datasource

我正在学习objective-c,我在本书中找到了以下代码。我有3个问题,

  1. 显然它通过实现两个必需的方法来符合协议,但为什么这样做没有在标题中写<UITableViewDataSource,UITableViewDelegate>
  2. 它符合哪个协议,UITableViewDataSourceUITableViewDelegate
  3. 为什么没有UITableView.delegate = self
  4. 这是代码,

    @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
    

    感谢您帮助理解这一点。

3 个答案:

答案 0 :(得分:2)

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

'&lt;'UITableViewDataSource,UITableViewDelegate'&gt;'在标题中只是向编译器指示您要在类中实现委托方法。如果您没有实现标记为 @required 的委托方法,您将收到警告,但由于大多数委托方法通常是 @optional ,因此您的代码将编译并运行精细。这并不意味着您不应该在标题中添加委托。

2. Which protocol it conforms, UITableViewDataSource or UITableViewDelegate?

默认只需要UITableViewDataSource,必须定义这两个函数

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


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

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

它就在那里,检查你从xib设置委托的xib。右键单击UITableView您将理解我的意思,如果不设置委托,上述方法将无效。

希望这有帮助。

答案 1 :(得分:1)

我同意@iphonic但是第一点。

'&lt;'UITableViewDataSource,UITableViewDelegate'&gt;'在您的标头中,表明编译器该类的实例确实响应了该协议。编译器将该信息用作类型匹配的附加信息。例如,为表视图设置delegate或dataSource属性时。

如果你不写'&lt;'UITableViewDataSource,UITableViewDelegate'&gt;'在类的标题中,你写了类似“tableView.dataSource = self”的东西,然后你会得到警告。使用IB或Storyboard时,不要编写该设置行。这就是你不必在类接口中包含协议声明的原因。

答案 2 :(得分:1)

ItemsViewController看起来像UITableViewController。对于通过UITableViewController访问的delegatedatasource个实例是默认的UITableViewself.tableView

因此没有明确标记协议实施<UITableViewDataSource,UITableViewDelegate>并设置self.tableView.delegate = selfself.tableView.datasource = self

但是,您可以选择将不同的实例设置为delegatedatasource,在这种情况下,您需要创建该实例,在其中实现所需的方法并将其分配给tableView

希望有所帮助!

相关问题