简体   繁体   中英

Collectionview inside tableview cell indexpath issue

Im using collection view inside my tableview cell.

For this I made a tableview cell class and inside it define collectionview . i have 2 secions for collectionview and Im getting the issue right now of setting indexpath as I required indexpath to check which index of collection clicked .

    #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];
}

At this point self.collectionViewFifa.indexpath = indexPath ; application is crashing saying:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionView setIndexpath:]: unrecognized selector sent to instance 0x7fa7aa016000'

Can anyone let me know what I'm doing wrong here?

You may try using cellForRowAtIndexPath instead of willDisplayCell ,

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

Suppose click event from cell

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

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

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

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM