【发布时间】:2017-06-08 05:14:24
【问题描述】:
我在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]];
}
【问题讨论】:
-
[NSIndexPath indexPathForRow: button.tag inSection:0],获取完整代码
[self.productCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow: button.tag inSection:0]]; -
@anbu-karthik :感谢您编辑答案
-
@Anbu.Karthik,非常感谢它的工作。我没有添加该部分,我想如果我不设置它,那么它将分配为零作为默认值。请把你的代码作为答案,我会接受它。
-
@SandeepBhandari - 没关系,我的兄弟,
-
@hotspring - 检查 sandeepbhandari 的答案也一样,接受那个答案
标签: ios objective-c uicollectionview