【问题标题】:Passing data from a collection view cell button to another view controller (Swift)将数据从集合视图单元格按钮传递到另一个视图控制器(Swift)
【发布时间】:2018-01-17 21:56:02
【问题描述】:

我在集合视图单元格中有一个按钮,按下时我想转到另一个视图控制器并将字符串传递给该视图控制器。我遇到的唯一问题是传递数据,我不知道如何检查按钮是从哪个单元格单击的。

extension UserViewController: UICollectionViewDataSource{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return self.posts.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PostCell", for: indexPath) as! UsersCollectionViewCell
        //cell.post = posts[indexPath.item]
        cell.User_Name.text = "\(self.posts[indexPath.item].firstname!) \(self.posts[indexPath.item].lastname!)"
        cell.Country.text = self.posts[indexPath.item].Country

        //user id is in the posts().uid

        return cell
    }

    //the segue is already made in the storyboard, i am trying to pass the user id in the function below

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Testing1"{
            var view = segue.destination as! ViewTheAccount

            //user_ID = self.posts[indexPath.item].firstname

        }
    }
}

【问题讨论】:

标签: ios swift xcode segue


【解决方案1】:

向你的单元格类添加一个字符串变量,并在 cellForRow 中给它你想要的 strValue

// btn action in cell

 @IBAction func btnClicked(_ sender: Any)
 {
    /// access cell and get it's string var , self = cell

    /// here use delegate to push another view controller with self.strValue
 }

【讨论】:

    【解决方案2】:

    试试这个(我假设这里只允许单选,另外我假设segues是通过选择一个单元格开始的):

        if segue.identifier == "Testing1" {
            var view = segue.destination as! ViewTheAccount
    
            if let itemIndex = collectionView.indexPathsForSelectedItems?.first?.item {
                let selectedItem = self.posts[itemIndex]
                // do here what you need
            }
        }
    

    【讨论】:

      【解决方案3】:

      因此,一种方法是在您正在使用的委托调用或回调中发送单元格。

      class SomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
      var collectionView:UICollectionView!
      
      collectionView(method that dequeues the cell){
            let yourcell = collection view.dequeue(...) as! SomeCell
            yourcell.somecallback = callback
      
      }
      
       func callback(cell: UICollectionViewCell){
         //To find out which cell it is just
      
         let indexPath = collection view.indexPathForCell(cell)
        //YOU NOW know which cell this was sent from.
      
      }
      
      }
      
      class SomeCell: UICollectionViewCell{
         var somecallback:((UICollectionViewCell)->())?
      
         func didPress(sender: UIButton){
              somecallback(self)
         }
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多