Ios 访问CollectionViewCell
我在Ios 访问CollectionViewCell,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我在collectionviewcell上添加了一个按钮,一旦用户单击该按钮,它将调用以下方法,但uviCollectionViewCell将返回nil - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if(collectionView == productCollectionView)
collectionviewcell
上添加了一个按钮,一旦用户单击该按钮,它将调用以下方法,但uviCollectionViewCell
将返回nil
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if(collectionView == productCollectionView)
{
__weak ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.addBtnOutlet.tag = indexPath.row;
[cell.addBtnOutlet addTarget:self
action:@selector(collectionViewCellAddButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
- (IBAction)collectionViewCellAddButtonPressed:(UIButton *)button{
NSLog(@"Add Button Clicked" );
// the following is nil
UICollectionViewCell *uviCollectionCell = [self.productCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathWithIndex:0]];
}
你只需要
如果你需要一个索引路径等于按钮标签的单元格,那么正如Karthik所说
UICollectionViewCell *uviCollectionCell = [self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:button.tag inSection:0]];
如果始终需要索引路径0,0处的单元格,则使用
UICollectionViewCell *uviCollectionCell = [self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
希望能有所帮助。您所需要的只是
如果你需要一个索引路径等于按钮标签的单元格,那么正如Karthik所说
UICollectionViewCell *uviCollectionCell = [self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:button.tag inSection:0]];
如果始终需要索引路径0,0处的单元格,则使用
UICollectionViewCell *uviCollectionCell = [self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
希望有帮助。如果你想不带标签的话
- (IBAction)collectionViewCellAddButtonPressed:(UIButton *)button{
NSLog(@"Add Button Clicked" );
UICollectionViewCell *uviCollectionCell = (UICollectionViewCell *)button.superview;
// if this is return nil then use like below as i am not sure it will retuen contentview for superview
UICollectionViewCell *uviCollectionCell1 = (UICollectionViewCell *)button.superview.superview;
}
如果你想不带标签的话
- (IBAction)collectionViewCellAddButtonPressed:(UIButton *)button{
NSLog(@"Add Button Clicked" );
UICollectionViewCell *uviCollectionCell = (UICollectionViewCell *)button.superview;
// if this is return nil then use like below as i am not sure it will retuen contentview for superview
UICollectionViewCell *uviCollectionCell1 = (UICollectionViewCell *)button.superview.superview;
}
[NSIndexPath indexPathForRow:button.tag安全设置:0],用于完整代码
[self.productCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:button.tag安全设置:0]代码>@anbu karthik:谢谢你编辑答案,我保留了你的编辑,但不确定OP的要求是什么,因此保留了一个注释welll@Anbu.Karthik,非常感谢,它成功了。我没有添加这个部分,我想如果我不设置它,那么它会默认指定零。请把你的代码作为答案,我会接受它。@SandepBhandari-没问题,我的兄弟,@Hotstring-检查SandepBhandari的答案也一样,接受这个答案[NSIndexPath indexPathForRow:button.tag Instition:0],获取完整的代码[self.productCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:button.tag Instition:0]代码>@anbu karthik:谢谢你编辑答案,我保留了你的编辑,但不确定OP的要求是什么,因此保留了一个注释welll@Anbu.Karthik,非常感谢,它成功了。我没有添加这个部分,我想如果我不设置它,那么它会默认指定零。请把你的密码作为答案,我会接受它。@Sandeppebhandari-没关系,我的兄弟,@HotSping-检查Sandeppebhandari的答案也一样,接受这个答案