0

ここには、UICollectionViewのために書いたコードがあります。このUICollectionViewではUIButtonを使用していますが、ボタンがクリックされた場所のセルのインデックスを知りたいと思います。ボタンタップでindexPath.item/rowの値が変化していますが、正しい値が得られません。UICollectionVIew indexPath.item/rowが不正な値を返す

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
    frpCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout]; 
    [frpCollectionView setDataSource:self]; 
    [frpCollectionView setDelegate:self]; 

    layout.minimumInteritemSpacing = 0; 
    layout.minimumLineSpacing = 0; 
    frpCollectionView.backgroundColor=[UIColor whiteColor]; 
    [frpCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 

    [self.view addSubview:frpCollectionView]; 
    [frpCollectionView mas_makeConstraints:^(MASConstraintMaker *make) 
    { 
     make.left.and.right.equalTo(self.view); 
     make.top.equalTo(self.view); 
     make.bottom.equalTo(self.view); 
    }]; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return frpTitleArray.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    frpCollectionViewCell = [frpCollectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 
    frpCollectionViewCell.layer.borderWidth=0.25f; 
    frpCollectionViewCell.layer.borderColor=[UIColor lightGrayColor].CGColor; 

    UIButton *setFrpPriceButton = [UIButton new]; 
    setFrpPriceButton.backgroundColor = UIColorFromRGB(0x2196f3); 
    [setFrpPriceButton setTitle:@"SET PRICE" forState:UIControlStateNormal]; 
    [setFrpPriceButton addTarget:self action:@selector(setFrpPriceClick) forControlEvents:UIControlEventTouchUpInside]; 
    setFrpPriceButton.tag = selectedCellIndex; 

    [frpCollectionViewCell addSubview:setFrpPriceButton]; 
    setFrpPriceButton.titleLabel.font = [UIFont boldSystemFontOfSize:10]; 
    setFrpPriceButton.clipsToBounds = YES; 

    [setFrpPriceButton mas_makeConstraints:^(MASConstraintMaker *make) 
    { 
     make.top.equalTo(frpButtonLabel); 
     make.width.equalTo(frpCollectionViewCell).dividedBy(3); 
     make.right.equalTo(frpCollectionViewCell); 
     make.height.equalTo(frpTitleLabel); 
    }]; 
    return frpCollectionViewCell; 
} 
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)displaySpecialityCollectionView 
{ 
    return 1; 
} 

- (CGSize)collectionView:(UICollectionView *)displaySpecialityCollectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height/2); 
} 

-(UIEdgeInsets)collectionView:(UICollectionView *)displaySpecialityCollectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
{ 
    return UIEdgeInsetsMake(0,0,0,0); 
} 

- (void)collectionView:(UICollectionView *)displaySpecialityCollectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedCellIndex = (int)[indexPath row]; 
} 
+1

'setFrpPriceClick'メソッドのコードを追加します。 –

+1

なぜ 'setFrpPriceButton.tag = selectedCellIndex;' setFrpPriceButton.tag = indexPath.row;の代わりに 'setFrpPriceButton.tag = selectedCellIndex;を設定していますか? –

+0

NSLog(@"行番号は%@ "、setFrpPriceButton.tag); – Developer

答えて

1

を: -

変更

setFrpPriceButton.tag = selectedCellIndex; 

また、

、この方法でアクセスボタンのクリックイベントこのような "setFrpPriceClick": -

-(void)setFrpPriceClick:(id)sender { 
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[sender tag] inSection:0]; 
    NSLog(@"row number is %@",indexPath.row); 
} 
0

セルを毎回作成するのではなく、サブクラス化する必要があります。しかし、いずれにせよ、試してみてください。

[setFrpPriceButton addTarget:self action:@selector(setFrpPriceClick:) forControlEvents:UIControlEventTouchUpInside]; 
setFrpPriceButton.tag = indexPath.row; 

あなたの方法は、このなります。ただ、 "cellForRowAtIndexPath" の方法で、この行を変更

-(void)setFrpPriceClick:(id)sender { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[sender tag] inSection:0]; 
     NSLog(@"row number is %@",indexPath.row); 
    } 
+0

可能であれば、サンプルコードでセルをサブクラス化する方法を教えてください。 – Developer

+0

@CodeGuruここで便利な例が見つかりますhttp://stackoverflow.com/questions/14530416/how-to-create-uicollectionview-未使用のストーリーボード –

+0

私の答えのコードスニペットは、サブクラス化しなくても動作するはずです。@ CodeGuruを試しましたか? –

関連する問題