What's the difference between data source and delegate?

后端 未结 6 701
离开以前
离开以前 2020-11-28 07:02

I have a fundamental question related to Cocoa frameworks design patterns.

What\'s the difference between delegate and data source?

Both of them cou

6条回答
  •  借酒劲吻你
    2020-11-28 07:23

    Both are Protocol, now the main intension of Protocol is to keep an universal coding practice, or same coding practice for all(to my understanding). Suppose I am creating a tableView without a UITableViewDataSource & UITableViewDelegate, I would create the tableView such a way that you woud not. Thats where Protocol comes, apple created some set of rule or protocol and everybody have to follow that. Now DataSource & Delegate are obviously Protocol, seeing the name you could understand DataSource deals with something like numberOfRowsInSection, cellForRowAtIndexPath, numberOfSections of tableView where some kind of data is being taken/processed, and Delegates are didSelectRow, willSelectRow, heightForRow etc of tableView where its related with some kind of UI change/action. So its just naming convention nothing hypothetical to keep the task separate. As @kubi said earlier: The datasource supplies the data, the delegate supplies the behaviour.

提交回复
热议问题