Вот код, который я написал для 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
. - person Nirav D   schedule 17.06.2016setFrpPriceButton.tag = selectedCellIndex;
вместоsetFrpPriceButton.tag = indexPath.row;
- person Jayesh Thanki   schedule 17.06.2016NSLog(@"row number is %@",setFrpPriceButton.tag);
? - person Jayesh Thanki   schedule 17.06.2016frpCollectionViewCell
является собственностью? Каждый раз, когда вызываетсяcollectionView:cellForItemAtIndexPath:
, значение будет другим! - person Larme   schedule 17.06.2016setFrpPriceButton.tag = indexPath.row;
+ удалить@property (zzz) UICollectionViewCell * frpCollectionViewCell;
(или ассимилировать) и использовать толькоUICollectionViewCell *frpCollectionViewCell = [frpCollectionView dequeueReusableCellWithReuseIdentifier:ZzZ
Это должно решить некоторые ваши проблемы. По-прежнему существует проблема повторного использования... Нам также нужен кодsetFrpPriceClick
, который явно должен бытьsetFrpPriceClick:
(чтобы получить тег от отправителя). - person Larme   schedule 17.06.2016setFrpPriceClick
? С таким кодом:-(IBAction)setFrpPriceClick:(id)sender{UIButton *button = (UIButton *)sender; NSLog(@"CellRow: %ld", [sender tag];}
иZzZ action:@selector(setFrpPriceClick:) ZzZ
? - person Larme   schedule 17.06.2016