0

The problem is i have a UICollectionView and i have a custom cell inherited from UICollectionViewCell.Now what i have on UICollectionView is two buttons left and right when i click the right button the cell should change and the next one should come in picture.The Cell has a TextView.And on click of right button the next cell(with textview) should be in picture.similarly on click of left it should display previous one.

Thanks and best regards.

CellforRow:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.myCV) {
        myCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];

        if (cell == nil)
        {
            NSArray *xibPath = [[NSBundle mainBundle] loadNibNamed:@"myCollectionView" owner:self options:nil];
            cell = [xibPath objectAtIndex:0];
        }
        return cell;
    }
    else if (collectionView == self.myCV2)
    {
        myCC2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL2" forIndexPath:indexPath];

        if (cell == nil)
        {
            NSArray *xibPath = [[NSBundle mainBundle] loadNibNamed:@"myCC2" owner:self options:nil];
            cell = [xibPath objectAtIndex:0];
        }
        return cell;
    }
    return 0;
}
0

1 Answer 1

0
-(void)moveCell:(UIButton*)sender
{
    if (sender.tag==0) {
        //move left
        if (count>0) {
            count--;
            NSLog(@"count == %i",count);
            [self snapToCellAtIndex:count withAnimation:YES];//paas index here to move to.
        }
    }else{
        //move right
        if (count<noOfCells-1) {
            count++;
            NSLog(@"count == %i",count);
            [self snapToCellAtIndex:count withAnimation:YES];//paas index here to move to.
        }
    }
}

- (void) snapToCellAtIndex:(NSInteger)index withAnimation:(BOOL) animated
{
    NSIndexPath *IndexPath = [NSIndexPath indexPathForRow:index inSection:0];
    UICollectionViewCell *cell= [_myCollectionView cellForItemAtIndexPath:IndexPath];
    [_myCollectionView scrollToItemAtIndexPath:IndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:animated];
    UITextView *txtView=(UITextView*)[cell viewWithTag:21];
    [txtView becomeFirstResponder]; //in current cell with index
}

use your custom cell type at this line

UICollectionViewCell *cell= [collectionView cellForItemAtIndexPath:IndexPath];

Hope this will help

2
  • you can easily get cell using collectionView cellforrowatIndexPath like tableView Commented Jun 13, 2013 at 7:12
  • Thank You So much for your Efforts. +1
    – user2478518
    Commented Jun 13, 2013 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.