【问题标题】:segue parse data from a tableviewcontroller to anothersegue 将数据从 tableviewcontroller 解析到另一个
【发布时间】:2016-03-14 14:39:28
【问题描述】:

我有一个带有搜索栏的表格视图。搜索栏返回用户名包含搜索栏中文本的 PFUser。该部分工作正常,获取的用户显示在表格视图中。当用户点击 tableview 单元格时,我想转到另一个视图控制器,其中包含用户点击已发布的图像表。我设置了 segue,下面是我的代码:

//usersSearched contains all the PFUsers whose usernames match the text in search bar 
       var usersSearched = [PFObject]()

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// showImages is the segue triggered when cell is tapped
    if segue.identifier == "showImages" {

        let pointInTable: CGPoint = sender!.convertPoint(sender!.bounds.origin, toView: searchResultTable)
        let cellIndexPath = searchResultTable.indexPathForRowAtPoint(pointInTable)

        let showImages = segue.destinationViewController as! ShowImagesViewController

        let userSelected = usersSearched[cellIndexPath!.row]

        let getImagesQuery = PFQuery(className: "allImages")

        getImagesQuery.whereKey("userId", equalTo: userSelected["userId"]!)

        getImagesQuery.findObjectsInBackgroundWithBlock { (posts, error) -> Void in

            if let posts = posts {

                showImages.pictureFile.removeAll(keepCapacity: true)

                for post in posts {

                    if let file = post["imageFile"] as? PFFile {

                        showImages.pictureFile.append(file)

                    } else {

                        let replacementImageData = UIImageJPEGRepresentation(UIImage(named: "whiteBackground.jpg")!, 0.8)

                        let replacementImageFile = PFFile(name: "replacementImageFile.jpeg", data: replacementImageData!)

                        showImages.pictureFile.append(replacementImageFile)
                    }
                  }
                }
              }
            }

以上代码来自包含搜索栏和tableview的视图控制器

以下是显示图像的视图控制器的代码:

    var pictureFile = [PFFile]()

    @IBOutlet var imageTableView: UITableView!


func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return pictureFile.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("imageCell") as! imageTableViewCell

        pictureFile[indexPath.row].getDataInBackgroundWithBlock { (fileData, error) -> Void in

            if let downloadedImage = UIImage(data: fileData!) {

                cell.imageHere.image = downloadedImage
            }
        }

        return cell
    }

所以我的问题是出现的图像不属于被点击的 PFUser。并且在视图控制器上自动提供了一个展开转场,该控制器在导航栏上使用典型的后退按钮显示图像。当我单击它时,它确实会返回搜索控制器,但是,当我在搜索栏中键入以再次搜索时,它要么不返回任何内容,要么返回不包含搜索栏文本的 PFUser。我尽力解释一切,我知道这听起来太混乱了,但请帮忙!谢谢

【问题讨论】:

    标签: swift uitableview parse-platform uisearchbar


    【解决方案1】:

    我猜你知道如何将图像放入 tableViewController。如果没有,请查看如何制作 tableViewCellClass。

    您将要使用 didSelectRowAtIndexPath(这将检测用户点击了哪个单元格)。在获取用户的 tableView 中,您不需要拉任何图像数据...这将在您填充图像 tableVC 时发生。

     // Delete any text within < > and replace with your own code
    
    var selectedCellText = ""
    
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
    
            // This gets the textLabel (in your case, the username) of the selected cell
            let cellIndexPath = tableView.indexPathForSelectedRow!
            let selectedCell = tableView.cellForRowAtIndexPath(cellIndexPath)! as UITableViewCell
    
    
    
    
        // Here you can add any details before you go to the next viewController, such as sending over the username of the selected user to the next viewController
    
        // This sets the variable above to the username, you will use it in prepare for segue
        selectedCellText = selectedCell.textLabel!.text!
    
        self.performSegueWithIdentifier("< identifier >", sender: self)
    
    
    
    }
    
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    
    
            let destinationVC : < viewController that you want to go to > = segue.destinationViewController as! < viewController that you want to go to >
    
            // destVC.username refers to a variable that has to be created in the next view controller. Once you present the next VC, you can check to make sure that "username" = < a username available in the database > and load that user's images
            destinationVC.username = selectedCellText
    
    
    
    }
    

    此时,当用户点击 tableView Cell 时,会出现下一个表格,您可以将图像加载到该 tableView 的单元格中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多