我是新的目标c。我想创建一个像android应用程序商店一样的布局设计。这意味着,我有一个tableview和tableview的每一行有一个collectionview,还有一件事,我使用xcode7,我想创建一个故事板的设计。当我应该创建一个这样的设计类型的每个tableview行显示collectionview但我的问题是,每个collectionview有保存数据。但我想要在每一行中使用不同的数据。我去扔很多教程,但我不明白。任何人都可以帮助我,如何创建这种类型的设计?现在我在viewcontroller.m文件中创建tableview数据库和委托方法,并将集合视图的datasourse和委托方法用于自定义tableview cells.m类。请任何一位协助我在每个tableview行下自定义Collectionview
0
A
回答
0
每个collectionview都有保存数据。但我想不同的数据中的每一行
因为tableViewCell您的收藏视图的datasourse和委托方法same.maybe它不是确定以确定数据源和tableViewCell委托执行中,你可以委托它到你的ViewController,并且每个tableViewCell都有不同的实现。
我试图执行演示,它可以工作,但我认为可能有更好的方法。这是我的代码
Cell.h
//Cell.h
#import <UIKit/UIKit.h>
@class MTTableViewCell;
@protocol MTTableViewCellDelegate <NSObject>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell;
@end
@interface MTTableViewCell : UITableViewCell<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic, weak) id<MTTableViewCellDelegate> delegate;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) NSInteger index; // [tablview indexPathForCell:] not work before cell is render
@end
Cell.m
//Cell.m
#import "MTTableViewCell.h"
@interface MTTableViewCell()
@end
@implementation MTTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
}
return self;
}
#pragma mark -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (_delegate && [_delegate respondsToSelector:@selector( collectionView:numberOfItemsInSection:InTableViewCell:)]) {
return [_delegate collectionView:collectionView numberOfItemsInSection:section InTableViewCell:self];
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath {
if (_delegate && [_delegate respondsToSelector:@selector( collectionView:cellForItemAtIndexPath:InTableViewCell:)]) {
return [_delegate collectionView:collectionView cellForItemAtIndexPath:indexPath InTableViewCell:self];
}
return nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(50, 50);
}
@end
ViewController.m
#import "ViewController.h"
#import "MTTableViewCell.h"
@interface ViewController()<UITableViewDataSource,UITableViewDelegate,MTTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 72.0;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MTTableViewCell *cell = (MTTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.index = indexPath.row;
cell.delegate = self;
[cell.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CCell"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:( NSIndexPath *)indexPath {
MTTableViewCell *mtCell = (MTTableViewCell *)cell;
if (!mtCell.collectionView.superview) {
[mtCell addSubview:mtCell.collectionView];
mtCell.collectionView.delegate = mtCell;
mtCell.collectionView.dataSource = mtCell;
}
}
#pragma mark - MTTableViewCellDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell {
UICollectionViewCell *cCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CCell" forIndexPath:indexPath];
cCell.backgroundColor = [UIColor redColor];
return cCell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell {
[_tableView indexPathForCell:cell];
return cell.index
;
}
@end
希望这可以帮助你:)
相关问题
- 1. 定义每个collectionView的collectionView.tag
- 2. Collectionview或tableview?
- 3. 自定义三角UICollectionviewCell在collectionview
- 4. 自定义空TableView
- 5. 我有一个tableview与多个单元格在它和每个单元格有tableview,collectionview,tableview,就像那
- 6. 每个tableview组中的单元格的自定义数量
- 7. 在RelativeLayout中定位自定义TableView
- 8. ios,自定义tableView或tableView控制器
- 9. TableView和CollectionView:调整tableView和collectionView的高度
- 10. 使用自定义collectionView为UITabBarController
- 11. 自定义tableview单元格
- 12. 自定义TableView错误
- 13. TableView自定义单元格
- 14. 自定义TableView问题
- 15. 自定义tableview和highlightTextColor
- 16. 在TableView之外配置自定义TableViewCell?
- 17. 在自定义TableView Cell中的UIWebView
- 18. RxSwift与CollectionView/TableView输入/ textfields
- 19. Collectionview自定义单元格布局/行为
- 20. 需要采取UIButton按下是在与自定义UITableViewCell嵌套TableView的行动
- 21. 我有自定义的tableview细胞和我在的tableview
- 22. CollectionView在Tableview中,与数据源错误
- 23. 如何在一个tableview中使用两个collectionview
- 24. 如何同时更新每个自定义tableview单元格数据?
- 25. 在tableView中使用多个自定义UITableViewCell(_cellForRowAt :)
- 26. Backbone.Marionette collectionview在el被定义时重置
- 27. 如何在用户点击自定义TableView行时弹出2个新按钮?
- 28. 下拉菜单为Android中的每个自定义ListView项目
- 29. 自定义对象的tableview迅速
- 30. 只有图像的自定义tableview
我想你的方式是对的。但是,我可以创建一个collectionview设计。你的协议想法是对的。您可以解释更多 – Abhinaba
您好Abhinaba〜您可能会注意到我已经在'-tableView:willDisplayCell:forRowAtIndexPath:'委托方法中设置了collectionView的委托和数据源,因为每个collectionView都与每个tableViewCell相关,所以我们需要tableViewCell已经可以使用(相关的委托和数据源方法被系统调用)。我只是试过了,你也可以用这个方法更新你的collectionview的设计。 :) – LiMengtian