UICollection视图didSelectItemAtIndexPath不能正常工作

时间:2018-07-14 06:38:33

标签: ios objective-c xcode uicollectionview

我在uiviewcontroller中创建了简单的collectionview,但是didSelectItemAtIndexPath并不总是对单元格的触摸做出反应。 DataSource和Delegates连接在情节提要板上。例如,如果我触摸我的所有单元格,则不会返回所有单元格,甚至2或3个单元格也无法单击。

下面是我的代码

//TestCollectionView.h
    #import <UIKit/UIKit.h>

    @interface TestCollectionView : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

    @property (strong, nonatomic) IBOutlet UICollectionView *myCollection;

    @end

TestCollectionView.m

#import "TestCollectionView.h"
#import "TestCell.h"

@interface TestCollectionView ()

@end

@implementation TestCollectionView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
      TestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"testCell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor redColor];

    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 20;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"slected index is %ld",(long)indexPath.row);

}


@end

0 个答案:

没有答案