ios - UIViewController 的模式可以显示为 UITableViewController 或 UICollectionViewController

标签 ios cocoa-touch uitableview design-patterns uicollectionview

在我的应用程序中,我有一个 ViewController,它将在 iPhone 上将内容显示为 UITableViewController,在 iPad 上将显示为 UICollectionViewController,我想要一个MYContentViewController 分别显示正确的 ViewController。 这是多重继承的理想情况,我有一个 MYContentTableViewControllerMYContentCollectionViewController,它们都继承自 UITable/CollectionViewControllerMYContentViewController 但显然 Cocoa 不支持多重继承,那么最好的解决方案是什么?

此外,我不想只拥有一个带有 UITableViewUICollectionViewMyContentViewController,因为从 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/

相关文章:

如果数字是 int,ios 去掉小数位

html - iOS 上 HTML 中不可选择的文本

cocoa - iCloud/CloudKit 用户的唯一标识符

cocoa-touch - 没有 nil 终止的可变参数函数

objective-c - 将 NSString 转换为 NSDictionary

ios - 自上次 Xcode 更新以来的 "Direct comparision of a string literal"警告

ios - YouTube 嵌入视频无法启动

ios - 带阴影的 UITableView

iphone - 具有可变宽度单元格的 UITableView

ios - 你能帮忙解决 data.entry.init 问题吗?