【问题标题】:How to add cell to dynamic collection view using swift如何使用 swift 将单元格添加到动态集合视图
【发布时间】:2017-04-20 05:38:10
【问题描述】:

在该图像的顶部,您可以看到一些图像和加号按钮,我想在我的应用程序中这样做,所以我使用集合视图来获取此视图,一切正常,但我无法将单元格添加到我的动态集合视图中一个帮我完成这个过程

我的视图控制器中的代码:

 import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
import SwiftElegantDropdownMenu
class EditPendingViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate{



var pendingImageString:[String]!

 @IBOutlet var editPendingCollectionvIEW: UICollectionView!
 override func viewDidLoad() {
        super.viewDidLoad()

 }

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return pendingImageString.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("editPendingCell", forIndexPath: indexPath) as!MypendindeditCollectionViewCell
        let img:NSString = pendingImageString[indexPath.item]
        cell.editImage.sd_setImageWithURL(NSURL(string: imageApikey + (img as String)))
        cell.contentView.layer.borderColor = UIColor.lightGrayColor().CGColor
        cell.contentView.layer.borderWidth = 1.0
        return cell
    }

}

我还没有完成任何将单元格添加到我的集合视图的过程,我只是从服务器加载了数据。现在帮助添加单元格

【问题讨论】:

    标签: ios insert swift2 uicollectionview cell


    【解决方案1】:
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pendingImageString.count + 1 // + 1 for last cell
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell : CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
         cell.btn.tag = indexPath.row
    
         if indexPath.item = pendingImageString.count {
           cell.imageview = //your static image that is shown in last cell
           cell.cancelButton.isHidden = true
         }
         else{
             let img:NSString = pendingImageString[indexPath.item]
             cell.editImage.sd_setImageWithURL(NSURL(string: imageApikey + (img as String)))
             cell.cancelButton.isHidden = false
         }
    
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
         if indexPath.item = pendingImageString.count{
           // update your array with new image file
         }
    }
    
    // cancel button action
    func buttonClicked(sender: UIButton?) {
        let tag = sender.tag
            // remove object from array and reload collectionview        
    }
    

    【讨论】:

    • 我的代码在 obj-c 中可用,确保你已经在 swift 中完成
    • 我需要从我想要执行操作的单元格向我的集合视图添加其他单元格。了解吗??
    • 等等,让我在 swift 中为简单的事情更改我的代码工作
    • 动作目标是你的取消按钮动作,如果你是从情节提要中完成的,那么不要写。检查更新的答案。
    • 在该图像中,您可以看到五个图像,然后有一个单元格具有加号图像,该图像是从该单元格中额外插入的单元格,我应该执行操作
    【解决方案2】:

    点击集合视图的最后一个单元格添加逻辑以在数据库中输入您的产品,然后重新加载集合视图

       func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if indexPath.row == pendingImageString.count - 1
            {
                //TODO - add item to database and reload collectionview
            }
        }
    

    【讨论】:

    • 我正在询问如何将附加单元格插入到我要执行操作的单元格中的动态集合视图中,请理解我的问题
    • 这就是我在点击添加按钮时所说的,您将添加逻辑,将您的项目添加到数据库中,并且由于您在重新加载后从数据库加载对象,集合视图将填充新单元格
    • 我的视图控制器中没有按钮每个进程都应该由集合视图完成。我现在只是将数据从服务器加载到集合视图另外我想将单元格添加到那个集合视图就是它。如何做到这一点
    • 在集合视图中显示所有数据,就像您在图像中显示的那样,并且在集合视图中有一个额外的单元格,我指的是单击这个额外单元格时的添加按钮,您可以将项目添加到数据库并重新加载集合视图
    猜你喜欢
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多