How might I create one UITableView with two data sources

泄露秘密 提交于 2020-01-25 00:12:26

问题


I have a UIViewController with one UITableView. But that tableView has two data sources. Basically a UIButton would decide which data source is active. How might I do that? there is no such method as

 [self.tableView reloadData:myDataSourceArray];

Which would help a great deal towards discriminating which data source to use and which UITableViewCell extension to use. So how might I go about this?

I say button to keep it simple, but in reality, dataSource_1 is preloaded from server while dataSource_2 is loaded by a UISearchBar.


回答1:


You should use a BOOL to track which data source is active. This is necessary because you are also using two different extended classes of UITableViewCell. Say

BOOL dataSourceOneIsActive;

When you do cellForRowAtIndexPath or numberOfRowsInSection you can check

if(dataSourceOneIsActive)

to determine which count and which cell and data source to use.

On the other hand, if you use the NSMutableArray idea, then you will have your work cut out for you when it comes time to decide which cell type to use.




回答2:


How about creating a mutable array, say, objectsToDisplayArray and then on button click, assign which source you want then just call reloadData. If you want to change the source just remove all objects from objectsToDisplayArray, add the new data, then reload again.



来源:https://stackoverflow.com/questions/26091494/how-might-i-create-one-uitableview-with-two-data-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!