如何在uicollectionview中获取当前的cell.contentview。如何在uicollectionview单元格中添加子视图

时间:2017-01-07 05:21:50

标签: ios objective-c swift uicollectionview

您好我的应用程序我想在cell.contentview中添加特定视图现在情况是我可以添加此代码。原因是,如果我将此代码放在cellforitematindexpath上,它将被添加到最后一个单元格(indxpath.row)中,原因是在cellforrowatindexpath中,最后一个单元格将被加载,因此视图将被添加到最后一个单元格中。虽然我希望它在当前时间显示的那个细胞上

这是我的代码。只需要添加子视图

 let cell : pdfImageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "pdfImageCell", for: indexPath) as! pdfImageCell


 cell.contentView.addSubview(userResizableView1)

3 个答案:

答案 0 :(得分:0)

您可以在UIView方法中添加awakeFromNib()。它只会拨打一次电话。

class pdfImageCell : UICollectionViewCell {

    //you can add here
    override func awakeFromNib() {

        var userResizableView1 = UIView()
        self.contentView.addSubview(userResizableView1)
    }

}

答案 1 :(得分:0)

UICollectionView中获取如下所示的可见单元索引。如果多个单元格在屏幕上可见,则可以看到它们可见,因此选择了所需的单元格。

var visibleCurrentCell: IndexPath? {
    for cell in self.collectionView.visibleCells {
        let indexPath = self.collectionView.indexPath(for: cell)
        return indexPath
     }    
     return nil
}

然后从索引中获取单元格并在该单元格中添加子视图。

答案 2 :(得分:0)

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths

这是一个在collectionView

中重新加载特定indexPath的方法

您可以在点击时更新您的数据源,您的cellForItemAtIndex将从您的数据源知道此次必须拥有子视图

if (datasourcearray[indexpPath.row].isSubViewToBeAdded)
{
    self.contentview.addsubview(subViewToBeAdded)
}
相关问题