gpt4 book ai didi

ios - UICollectionVIew indexPath.item/row 返回不正确的值

转载 作者:行者123 更新时间:2023-11-29 00:53:17 34 4
gpt4 key购买 nike

这是我为 UICollectionView 编写的代码。在此 UICollectionView 中,我使用 UIButton,我想知道单击按钮的单元格的索引。点击按钮时 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];
}

最佳答案

只需在“cellForRowAtIndexPath”方法中更改此行:-

改变

setFrpPriceButton.tag = selectedCellIndex;

setFrpPriceButton.tag = indexPath.row;

此外,在方法“setFrpPriceClick”中访问按钮的点击事件,如下所示:-

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

关于ios - UICollectionVIew indexPath.item/row 返回不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879403/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com