问题
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