从现有的tableView数据源和控制器创建collectionView

时间:2016-10-04 05:05:31

标签: ios objective-c uitableview uicollectionview

所以我一直致力于一个名为Readium ePub 框架,他们最近为IOS创建了一个SDK。

SDK是一个完整的工作项目,允许用户从tableView列表中选择一个ePub,选择一个后会为您提供有关该ePub的元数据,之后您必须选择页面列表或脊椎项目选择其中一个页面后,它最终会进入ePub。

我希望通过在collectionView中打开ePub列表来简化此过程,并在选择ePub后直接进入该ePub的第一个/封面页面。

我想知道的是,是否可以使用viewController的{​​{1}}和数据源来创建新的tableView?我已经调整了这个项目以允许滑动导航。

1 个答案:

答案 0 :(得分:1)

可以很快完成。

1)您必须告诉控制器您要修改集合视图,因此.h文件中有两个选项:

a)如果您当前正在继承UITableView - 从UICollectionViewController继承您的ViewController

@interface YouViewControllerName : UICollectionViewController

b)如果继承自UIViewController ,只需设置 控制器 符合CollectionView 委托和dataSource方法像这样

@interface YouViewControllerName : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource> { ... }

2)然后您需要将表视图委托和数据源方法 替换为集合视图数据源和委托方法,因此您需要替换这样的东西:

旧:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ... }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { ... }

新:

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                           cellForItemAtIndexPath:(NSIndexPath *)indexPath { ... }

- (NSInteger)collectionView:(UICollectionView *)collectionView 
     numberOfItemsInSection:(NSInteger)section { ... }

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { ... }

因此,之前的委托/数据源方法中的内容可以保持相同,因为不需要更改逻辑。

您可能需要实现一些更具体的方法,并且可以设计正确的项目单元格大小等,但是当您的布局被集合视图替换时,这是很自然的。