我有一个带有搜索栏的tableview。搜索栏返回PFUsers,其用户名包含搜索栏中的文本。该部分工作正常,提取的用户显示在tableview中。当用户点击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轻拍。并且在视图控制器上自动提供展开segue,其在导航栏上显示具有典型后退按钮的图像。当我点击它时,它确实会回到搜索控制器,但是,当我输入搜索栏再次搜索时,它会返回任何内容或者不包含搜索栏文本的PFUser。我尽力解释一切,我知道这听起来太混乱,但请帮忙!感谢
答案 0 :(得分:0)
我猜你知道如何把图像放到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的单元格中