我在我的tableview
单元格中使用了集合视图。
为此,我制作了一个tableview
单元格类,并在其中定义collectionview
。我对collectionview
有2个分区,我现在正在设置问题indexpath
,因为我需要indexpath
来检查点击了哪个集合索引。
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(FifaPlanTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
}
interface FifaPlanCollectionView : UICollectionView
@property (nonatomic, strong) NSIndexPath *indexpath;
@end
@interface FifaPlanTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet FifaPlanCollectionView *collectionViewFifa;
- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath;
- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionViewFifa.delegate = dataSourceDelegate;
self.collectionViewFifa.dataSource = dataSourceDelegate;
self.collectionViewFifa.indexpath = indexPath;
[self.collectionViewFifa reloadData];
}
此时self.collectionViewFifa.indexpath = indexPath
;应用程序崩溃说:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionView setIndexpath:]: unrecognized selector sent to instance 0x7fa7aa016000'
有谁能让我知道我在这里做错了什么?
答案 0 :(得分:1)
您可以尝试使用cellForRowAtIndexPath
代替willDisplayCell
,
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FifaPlanTableViewCell *cell = (FifaPlanTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
[cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
}
答案 1 :(得分:0)
假设从单元格中单击事件
[cell.buttonInstance addTarget:self action:@selector(getIndexActionMethod:event :) forControlEvents:UIControlEventTouchUpInside];
- (void)getIndexActionMethod:(UIButton *)sender事件:(UIEvent *)事件 {
UITouch * touch = [[events allTouches] anyObject];
CGPoint touchPos = [touch locationInView:self.tableViewInstance];
NSIndexPath *indexPath = [self.tableViewInstance indexPathForRowAtPoint:touchPos];
}