UICollectionVIew indexPath.item/row 返回不正确的值
Posted
技术标签:
【中文标题】UICollectionVIew indexPath.item/row 返回不正确的值【英文标题】:UICollectionVIew indexPath.item/row returning incorrect value 【发布时间】:2016-06-17 10:34:25 【问题描述】:这是我为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];
【问题讨论】:
添加setFrpPriceClick
方法的代码。
你为什么设置setFrpPriceButton.tag = selectedCellIndex;
而不是setFrpPriceButton.tag = indexPath.row;
NSLog(@"行号为%@",setFrpPriceButton.tag);
@JayeshThanki 我也试过了,但它不起作用,我面临同样的问题。
这段代码NSLog(@"row number is %@",setFrpPriceButton.tag);
是用什么方法写的?
【参考方案1】:
只需在“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);
【讨论】:
【参考方案2】:您应该子类化您的单元格,而不是每次都重新创建它。但无论如何,请尝试:
[setFrpPriceButton addTarget:self action:@selector(setFrpPriceClick:) forControlEvents:UIControlEventTouchUpInside];
setFrpPriceButton.tag = indexPath.row;
你的方法应该是这样的:
-(void)setFrpPriceClick:(id)sender
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[sender tag] inSection:0];
NSLog(@"row number is %@",indexPath.row);
【讨论】:
如果可能的话,请告诉我如何使用示例代码对单元格进行子类化。 @CodeGuru 你会在这里找到有用的例子***.com/questions/14530416/… 我的答案中的代码 sn-ps 即使没有 subclassing 也应该可以工作。你试过@CodeGuru 吗?以上是关于UICollectionVIew indexPath.item/row 返回不正确的值的主要内容,如果未能解决你的问题,请参考以下文章