在我的应用程序中,我有一个 ViewController,它将在 iPhone 上将内容显示为 UITableViewController
,在 iPad 上将显示为 UICollectionViewController
,我想要一个MYContentViewController
分别显示正确的 ViewController。 这是多重继承的理想情况,我有一个 MYContentTableViewController
和 MYContentCollectionViewController
,它们都继承自 UITable/CollectionViewController
和 MYContentViewController
但显然 Cocoa 不支持多重继承,那么最好的解决方案是什么?
此外,我不想只拥有一个带有 UITableView
或 UICollectionView
的 MyContentViewController
,因为从 iOS7 开始,如果你使用一个实际的 UICollectionViewController。
目前我正在考虑使用协议(protocol)和“ParentViewController”是最好的方式但如果有一种方法可以不让“ParentViewController”与相关联,我会很高兴添加 subview Controller
:
@protocol MYContentViewControllerDelegate <NSObject>
- (void)didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
@end
@protocol MYContentViewControllerDataSource <NSObject>
- (MYDataProvider *)dataProviderForIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
- (NSInteger)numberOfSections;
@end
@protocol MYContentViewController <NSObject>
- (void)reloadData;
- (void)reloadSections:(NSIndexSet *)sections;
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths;
- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths;
- (void)insertItemsAtIndexPaths:(NSArray *)indexPaths;
@property (nonatomic, weak) id <MYContentViewControllerDataSource> dataSource;
@property (nonatomic, weak) id <MYContentViewControllerDelegate> delegate;
@end
MyContentViewController
将实现数据源和委托(delegate),因此它将提供所有数据并作用于 Collection 和 Table 的所有交互,并拥有“父” View Controller 的任何交互(如导航按钮相互作用)。 MyContentViewController
将为表和集合 addChildViewController
。
最佳答案
我要尝试的第一件事是将 UITableView
视为 UICollectionView
的一个易于模拟的特例,具有垂直布局和全宽单元格。
然后您可以仅使用具有两种布局和单元格类型的常规 UICollectionViewController
。其中一种布局/单元格类型可以设计为模仿 UITableView/UITableViewCell
。
关于ios - UIViewController 的模式可以显示为 UITableViewController 或 UICollectionViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745247/