in some Special Situation, i will use two different class to realize the delegate
and the dataSource
method.
for example:
in my project, some data contain by a instance(let's call it Global user
), such as user's article, user's comment, user's product...
i make a instance Class for the Global user. in this class , i have some NSMutableArray
for user's data,when user login success.
i request his data from my data server and store in these data Array.
After that, when the user want to look his product, he will click some button like my product
, then push to MyProductListVC
, in the MyProductListVC
, i have a tableView
init to show his products, but i don't want to request his products data from my data server again, because i have all his product data in my Global user Class,so i make the tableView
's dataSource
to the Global user class, and make the delegate
to the MyProductListVC
, and this logic works fine on my project for a long time. this is how i define delegates and datasources in diffrent classes.
About your second question:
Why we need seperation as delegates and datasources?
i answered this question based on my project, first ,i want all user's data focus on one Class, this way convenient for me to manage the user's data. second , when i need to change how show the data in the view, i only need to change the code in MyProductListVC
.
Here's what I think is the most important:
i can put all request method in my Global user's Class, i don't want too many request code in my MyProductListVC
and any other VC, because in my project, the VC only need to show data, don't need to request.