集合视图后的任何委托方法都准备好了吗?

时间:2015-10-23 17:56:38

标签: ios objective-c uiview uicollectionview

我想调用didSelectItemAtIndexPath:对于特定的索引路径,但我无法在cellForItemAtIndexPath中以编程方式调用它,因为收集尚未准备好,我将获取单元格为零。我们是否有任何委托方法或在集合视图准备好后调用的任何其他UIView方法?

我已尝试过willDisplayCell:但它不是为相关工作而做的,找不到任何其他内容。

2 个答案:

答案 0 :(得分:0)

  

我想致电didSelectItemAtIndexPath:

唐'吨。这是一种委托方法。当用户选择项目时,它由运行时调用。你必须从不自己打电话。

答案 1 :(得分:0)

您必须使用手动逻辑以编程方式执行此操作。 :) 基本概念是获取所选单元格的索引并仅重新加载这些特定单元格。 声明全局var

NSMutableArray array_indexpath;
在你的选择方法中添加所选单元格的索引。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    array_indexpath=[[NSMutableArray alloc]init];
   [array_indexpath addObject:indexPath];
   [self.myCollectionView reloadItemsAtIndexPaths:array_indexpath];
}

并在您的cell forItemAtIndexPath方法的单元格中检查索引并根据需要重新加载。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShubhCalendarCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"ShubhCalendarCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor clearColor];
    if (array_indexpath.count !=0)
    {
        for (int i=0; i<[array_indexpath count]; i++)
        {
            if ([array_indexpath objectAtIndex:i] == indexPath)
            {
                cell.backgroundColor=[UIColor greenColor];
            }
        }
    }
return cell;
}

希望它有所帮助..快乐编码.. :)

相关问题