Collectionview里面的tableview单元索引路径问题

时间:2018-03-30 16:10:31

标签: ios objective-c uitableview uicollectionviewcell

我在我的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'

有谁能让我知道我在这里做错了什么?

2 个答案:

答案 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];

}