ios - 如何在uicollectionview中获取当前的cell.contentview。如何在 uicollectionview 单元格中添加 subview

标签 ios objective-c swift uicollectionview

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

这是我的代码。只需添加 subview

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


 cell.contentView.addSubview(userResizableView1)

最佳答案

UICollectionView 中获取可见单元格索引,如下所示。您还可以让多个单元格可见(如果它们在屏幕上可见),因此选择您想要的单元格。

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

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

关于ios - 如何在uicollectionview中获取当前的cell.contentview。如何在 uicollectionview 单元格中添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518102/

相关文章:

ios - "Send Message"键盘出现时不可见

ios - 在 5 月的应用程序中,即使应用程序被用户关闭/杀死,我也可以扫描 Eddystone 信标吗?

ios - 使用计时器更改注释上的图像 - Swift

ios - UILabel 永远不会在 viewDidLoad 中初始化

objective-c - isEqualTo : and isEqual:之间的区别

objective-c - NSObject 扩展 UIAlertViewDelegate 崩溃?

ios - 在 UITableview 的 headerview 中将动态图像添加到 UIView

arrays - Swift 中的数组可变性追加

ios - UITabBar 最佳实践

ios - Office 365 SharePoint 列表通知新项目