为什么我的UICollectionViewCell触摸事件会被偏移?

时间:2014-11-17 14:49:05

标签: ios objective-c uicollectionview

我有一个使用Flow Layout的UICollectionView。在这一点上,我并没有做任何特别的事情。我只是以编程方式添加视图和UICollectionViewCell。

当您触摸单元格时,它正在为位于其上方的单元格注册触摸事件。因此,如果您触摸第2行上的单元格,则上面的单元格实际上是注册触摸的单元格。

这是我设置的代码。

@interface KFYSearch() <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>

@property(nonatomic, strong)UISearchBar *searchBar;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSArray *collectionData;

@end

@implementation KFYSearch


-(id)initWithView:(UIView*)view
{
    self = [super init];

    if(self)
        self.view = view;

    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(_searchBar == nil)
    {
        self.view.frame = CGRectMake(20, 20, self.view.superview.frame.size.width - 40, self.view.superview.frame.size.height - 40);
        [self.view setBackgroundColor:[UIColor whiteColor]];


        [self createChildViews];
    }

}


-(void)createChildViews
{
    _searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(5, 5, self.view.frame.size.width - 10, 30)];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(_searchBar.frame.origin.x, _searchBar.frame.origin.y + (_searchBar.frame.size.height + 2), _searchBar.frame.size.width, self.view.frame.size.height - ((_searchBar.frame.origin.y + _searchBar.frame.size.height) + 10)) collectionViewLayout:flowLayout];

    _collectionView.dataSource = self;
    _collectionView.delegate = self;

    [_collectionView registerClass:[KFYKlickableCell class] forCellWithReuseIdentifier:@"Cell"];
    [_collectionView setBackgroundColor:[UIColor whiteColor]];

    [self.view addSubview:_searchBar];
    [self.view addSubview:_collectionView];

    [self getData];
}


-(void)getData
{
    KFYGetKlickablesAction *getKlickablesAction = [[KFYGetKlickablesAction alloc]init];
    _collectionData = [getKlickablesAction mockData];

    [_collectionView reloadData];
}


#pragma UICollectionView Delegate stuff;

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionData.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    KFYKlickableCell *cell= [_collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor redColor]];

    KFYKlickableVO *vo = [_collectionData objectAtIndex:indexPath.row];
    [cell renderKlickable:vo];

    return (UICollectionViewCell*)cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(60, 60);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KFYKlickableVO *vo = [_collectionData objectAtIndex:indexPath.row];

    NSLog(vo.tag);
}

#pragma UITextField Delegate stuff

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    //Keyboard becomes visible
    self.view.frame = CGRectMake(self.view.frame.origin.x,
                                  self.view.frame.origin.y,
                                  self.view.frame.size.width,
                                  self.view.frame.size.height - 215 + 50);   //resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    //keyboard will hide
    self.view.frame = CGRectMake(self.view.frame.origin.x,
                                  self.view.frame.origin.y,
                                  self.view.frame.size.width,
                                  self.view.frame.size.height + 215 - 50); //resize
}


@end

0 个答案:

没有答案