【发布时间】:2015-12-23 17:49:45
【问题描述】:
所以我有一个 UICollectionView 和一个自定义单元格,它们都显示出来并且效果很好。我在 viewDidLoad 中注册了这个类:
myCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
[myCollectionView setDataSource:self];
[myCollectionView setDelegate:self];
[myCollectionView setBackgroundColor:[UIColor myColor]];
[myCollectionView registerClass:[SBCustomCell class] forCellWithReuseIdentifier:@"Cell"];
在我的 cellForItemAtIndexPath 方法中,我将单元格出列并设置其属性并返回它:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
SBCustomCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[cell setName: "My Name"];
cell.image = [UIImage imageNamed:@"lol.png"];
cell.score = 100.0;
cell.backgroundColor=[UIColor whiteColor];
return cell;
}
这一切都很好,它会显示在 UI 中。我的问题是当我在 collectionView 上设置手势识别器时,当我长按某个单元格时,我希望能够访问它的属性。我正在尝试这样做:
-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {
CGPoint locationPoint = [longPressRecognizer locationInView:myCollectionView];
if (longPressRecognizer.state == UIGestureRecognizerStateBegan) {
NSIndexPath *indexPathOfMovingCell = [myCollectionView indexPathForItemAtPoint:locationPoint];
SBCustomCell *cell= [myCollectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPathOfMovingCell];
NSLog(@"%@",cell.name);
当我尝试访问我的任何自定义单元格的属性时,它在控制台中为 (null)。这是为什么?我做错了什么?
【问题讨论】:
标签: ios objective-c uicollectionview uicollectionviewcell