单击UIButton切换/更改UICollectionView中的单元格

时间:2013-06-12 13:31:40

标签: ios ios6 uicollectionview

问题是我有一个UICollectionView并且我有一个继承自UICollectionViewCell的自定义单元格。现在我在UICollectionView上的内容是当我单击右键时左右两个按钮单元格应该改变,下一个应该进入画面。单元格有一个TextView.And点击右键,下一个单元格(带有textview)应该在图片中。同样点击左边它应该显示前一个。

谢谢和最诚挚的问候。

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;
}

1 个答案:

答案 0 :(得分: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
}

在此行使用自定义单元格类型

UICollectionViewCell *cell= [collectionView cellForItemAtIndexPath:IndexPath];

希望这会有所帮助