【问题标题】:How to show multiple images in UICollectionView Swift如何在 UICollectionView Swift 中显示多个图像
【发布时间】:2016-06-15 18:34:55
【问题描述】:

这个扩展只在 CollectionView 中提供了一个 UIImageView。如何显示更多图像,取决于照片数组的大小。 Photos 数组包含图像的 Url 数组(字符串)。

extension FlatViewController: UICollectionViewDataSource{
        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return (flat.photos?.count)!
        }
        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCollectionViewCell

            cell.imageView.loadImageFromURL(NSURL(string: flat.image!)!)
            return cell
        }
    }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您可以简单地进入 Interface Builder 并根据需要将 ImageView elements 的数量放入您的 CollectionViewCell 中。然后创建IBOutlets 以引用您在CollectionViewController 中添加的ImageView 元素。然后在函数cellForItemAtIndexPath 中插入每个IBOutlet ImageView's 的图像URL。

    应该可以的。

    【讨论】:

      【解决方案2】:

      UICollectionViewCell 子类中,基于照片数组的子数组以编程方式创建UIImageViews。您可以通过设置两个协议并实现一个委托模式来做到这一点,其中您的 FlatViewController 是您的子类的委托。

      协议看起来像这样:

      protocol PhotoDelegate {
      
        var photos: [[NSURL]] { get set }
      
      }
      
      protocol HasPhotos {
      
        var delegate: PhotoDelegate! { get set }
      
      }
      

      委托模式将允许您从子类访问photos 数组。您应该设置出列单元变量的委托属性并将其转换为cellForRowAtIndexPath 中的子类。喜欢这样

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "<Insert Identifier>", for: indexPath) as! Subclass
      cell.delegate = self
      

      如果您有任何问题,请尽管提问!我喜欢提供帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-16
        • 1970-01-01
        • 2021-10-15
        • 2017-03-30
        • 1970-01-01
        相关资源
        最近更新 更多