Xcode UICollectionView如何通过indexpath禁用某些单元格

时间:2014-10-15 23:27:06

标签: ios objective-c xcode uicollectionview uicollectionviewcell

我一直试图弄清楚如何通过indexpath或使用collectionsview的cellsforitemsatindexpath方法来禁用集合视图中的单元格。我在下面有以下代码,但这只会禁用循环中的最后一个单元格。当我在Xcode中运行代码时,我还有以下图像。到目前为止,只有第三个单元格水平在顶部的第一行被禁用,但我想禁用或设置值以使顶行的前3个单元格水平。从下面的图片链接中,找不到的	 9'在第三个单元格上表示该值设置为nil。任何建议都表示赞赏。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionCell" forIndexPath:indexPath];

    if (cell != nil)
    {
        // May not need this
        NSInteger cellIndex = [myObjectsArray objectAtIndex:indexPath.row];

        for (int i = 0; i < rangeNumOfCellsToDisable; i++)
        {
            if (indexPath.row == i)
            {
                // If true, code goes here


            }
            else
            {
                // If not true, code that sets stuff to nil goes here


            }


        }


    }

    return cell;

}

截图图片 https://drive.google.com/folderview?id=0BzaP1abbReAhMTYtSEh3alFlaGs&usp=sharing

屏幕截图:https://drive.google.com/folderview?id=0BzaP1abbReAhVzhCeEY2eFZibFE&usp=sharing

1 个答案:

答案 0 :(得分:0)

嗨,请尝试这个解决方案。

我假设您希望禁用前三个单元格,或者您可以禁用任何单元格。

<。>文件中的

-@property(nonatomic,retain)NSMutableArray *disableArray;

in.m文件

disableArray=[[NSMutableArray alloc]init];

[disableArray addObject:@"0"];
[disableArray addObject:@"1"];
[disableArray addObject:@"2"];

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionCell" forIndexPath:indexPath];

        if (cell != nil)
        {
            // May not need this
            NSInteger cellIndex = [myObjectsArray objectAtIndex:indexPath.row];

            if([disableArray containsObject:[NSString stringwithformat:@"%d",indexPath.row]])
              {
                //the row which you want to disable do whatever you want over here...Hope this helps.
              }


        }

        return cell;

    }
相关问题