首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过协议和来自UICollectionViewCell内部UITableViewCell的委托传递数据以执行segue发送者?

如何通过协议和来自UICollectionViewCell内部UITableViewCell的委托传递数据以执行segue发送者?
EN

Stack Overflow用户
提问于 2020-07-05 14:47:59
回答 3查看 288关注 0票数 1

当然,我在TableView

  • Inside中安装了一个注册的UICollectionView

  • UICollectionViewCell。当UICollectionViewCell在UITableViewCell.xib中注册时,

  • 想要执行从VC(a)到VC(b)的分离,这样我就不能在.xib文件中使用performSegue(withIdentifier: String,发件人: Any),因为它不是从UIViewController

继承的。

我在StackOverFlow中做了一些搜索,通过创建一个定制的委托协议,我找到了执行segue的方法,我应用了该协议,除了发送者之外,一切看起来都很好!

我知道如何在没有协议的VC中使用它,但是在应用协议之后,我不知道如何使用它

我是Swift语言新手,所以请帮助我完成我的代码

下面是UITableViewCell.xib代码

代码语言:javascript
复制
protocol MyCustomCellDelegator {
    func cellWasPressed()
}


class AdminPList_TableViewCell: UITableViewCell {
    
    var delegate: MyCustomCellDelegator?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setupCell()
        getFProducts()
    }
    
    @IBOutlet weak var CollectionView: UICollectionView!
    
    func getFProducts() {
        productAPI.GetAllproducts { (appendThisProduct) in
            self.FP_Array.append(appendThisProduct)
            self.CollectionView.reloadData()
        }
    }
    
    var FP_Array : [productObject] = []    
}

extension AdminPList_TableViewCell : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func setupCell() {
        CollectionView.delegate = self ; CollectionView.dataSource = self

        CollectionView.register(UINib(nibName: "FproductsCell", bundle: nil), forCellWithReuseIdentifier: "FPcell")

//      CollectionView.register(UINib(nibName: "TproductsCell", bundle: nil), forCellWithReuseIdentifier: "TPcell")
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: self.CollectionView.frame.size.width-10, height: self.CollectionView.frame.size.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {  // make spacing between each cell
        return 10
    }

    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return FP_Array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let FPcell = CollectionView.dequeueReusableCell(withReuseIdentifier: "FPcell", for: indexPath) as! FproductsCell
        
        FPcell.UpdateFP_cell_Content(RetrivedProducts: FP_Array[indexPath.row])
        
        return FPcell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let selectedProduct = FP_Array[indexPath.row] // I want to pass it to sender in perform segue un ProductsLibrary Class

//      print (selectedProduct.productName)
        self.delegate?.cellWasPressed()
        
    }
}

帮助我通过selectedProduct在下面的协议中执行segue

下面是安装UITableView的VC(a):

代码语言:javascript
复制
class ProductsLibrary : UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupCell()
    }
    
    @IBOutlet weak var TableView: UITableView!

    
}



extension ProductsLibrary : UITableViewDelegate, UITableViewDataSource, MyCustomCellDelegator {
    
    func cellWasPressed(withData:  productObject) {
        performSegue(withIdentifier: "EditProduct", sender: self)
        
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let next = segue.destination as? ProductManagement{
            print ("Editing Product is Active")

              let product = sender as? productObject
                print(product) //it shows nil !!!
                next.EditingProduct = product
        }
    }
    
    func setupCell() {
        TableView.delegate = self ; TableView.dataSource = self
        
        TableView.register(UINib(nibName: "AdminPList_TableViewCell", bundle: nil), forCellReuseIdentifier: "PLcell")
    }
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    
        return 3
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                        
        let cell = tableView.dequeueReusableCell(withIdentifier: "PLcell", for: indexPath) as! AdminPList_TableViewCell
            
        cell.delegate = self
        
        
            return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return self.TableView.frame.size.height/3
    }
    

    
    
}
EN

Stack Overflow用户

发布于 2020-07-05 15:06:47

你好吗?

也许您的委托是零,这就是为什么您没有调用func 的原因。

实例化单元格后,需要在VC上设置委托。

如下所示:

代码语言:javascript
复制
AdminPList_TableViewCell.delegate = self

希望它能帮到你!

愉快编码=D

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62742218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档