One UITableView - Multiple DataSource, best design pattern?

前端 未结 1 634
小鲜肉
小鲜肉 2021-02-04 19:46

This seems like a typical problem, but I have a UITableView that has identical behavior for two separate data sources. What is the best way of going about designing the class hi

相关标签:
1条回答
  • 2021-02-04 20:05

    Be careful with your terminology here. A UITableView has something called a dataSource but you seem to be referring, essentially, to two different sets of data.

    In the case you're suggesting, in the table's dataSource (the object that adheres to the UITableViewDataSource protocol), I'd have three arrays.

    • currentlyViewedArray
    • datasetOneArray
    • datasetTwoArray

    In the dataSource methods, use the currentlyViewedArray as the source of the table's data.

    Then, set the currentlyViewedArray to whichever array you want to view:

    self.currentlyViewedArray = self.datasetOneArray;
    [theTableView reloadData];
    

    You can use the UISegmentedControl to switch between the two arrays.

    0 讨论(0)
提交回复
热议问题