简体   繁体   English

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

[英]Collectionview inside tableview cell indexpath issue

Im using collection view inside my tableview cell. 我在tableview单元格内使用集合视图。

For this I made a tableview cell class and inside it define collectionview . 为此,我制作了一个tableview单元格类,并在其中定义了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 . 我有2个部分用于collectionview ,我现在正在设置indexpath的问题,因为我需要indexpath来检查单击了哪个collection索引。

    #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 ; 此时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 , 您可以尝试使用cellForRowAtIndexPath代替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]; [cell.buttonInstance addTarget:self动作:@selector(getIndexActionMethod:event :) forControlEvents:UIControlEventTouchUpInside];

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

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

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

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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