tableview 单元格索引路径问题中的 Collectionview

Collectionview inside tableview cell indexpath issue

我在 tableview 单元格中使用集合视图。

为此我制作了一个 tableview 单元格 class 并在其中定义 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'

谁能告诉我我做错了什么?

您可以尝试使用 cellForRowAtIndexPath 而不是 willDisplayCell,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FifaPlanTableViewCell *cell = (FifaPlanTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
[cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
}

假设来自单元格的点击事件

[cell.buttonInstance addTarget:self 动作:@selector(getIndexActionMethod:event:) forControlEvents:UIControlEventTouchUpInside];

-(void) getIndexActionMethod:(UIButton*)sender 事件:(UIEvent *)events {

UITouch *touch = [[events allTouches] anyObject];

CGPoint touchPos = [touch locationInView:self.tableViewInstance];

NSIndexPath *indexPath = [self.tableViewInstance  indexPathForRowAtPoint:touchPos];

}