【问题标题】:Switch/Change the Cells in UICollectionView on click of a UIButton单击 UIButton 时切换/更改 UICollectionView 中的单元格
【发布时间】:2013-06-12 13:31:39
【问题描述】:

问题是我有一个UICollectionView,并且我有一个从UICollectionViewCell 继承的自定义单元格。现在我在UICollectionView 上拥有的是左右两个按钮,当我单击右键时,单元格应该会更改,并且下一个应该出现在图片中。单元格有一个 TextView。单击右键时,下一个单元格(带有 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;
}

【问题讨论】:

    标签: ios ios6 uicollectionview


    【解决方案1】:
    -(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];
    

    希望这会有所帮助

    【讨论】:

    • 你可以像tableView一样使用collectionView cellforrowatIndexPath轻松获取cell
    • 非常感谢您的努力。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多