gpt4 book ai didi

swift - segue 将数据从一个 tableviewcontroller 解析到另一个 tableviewcontroller

转载 作者:行者123 更新时间:2023-11-30 13:36:43 26 4
gpt4 key购买 nike

我有一个带有搜索栏的表格 View 。搜索栏返回用户名包含搜索栏中文本的 PFUser。该部分工作正常,并且获取的用户显示在 TableView 中。当用户点击表格 View 单元格时,我想转到另一个 View Controller ,其中包含用户点击的已发布的图像表。我设置了 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)
}
}
}
}
}

以上代码来自包含搜索栏和表格 View 的 View Controller

下面是我显示图像的 View Controller 的代码:

    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。 View Controller 上自动提供了展开序列,该 View Controller 使用导航栏上的典型后退按钮显示图像。当我单击它时,它确实返回到搜索 Controller ,但是,当我在搜索栏中键入内容再次搜索时,它要么不返回任何内容,要么返回不包含搜索栏文本的 PFUser。我尽力解释一切,我知道这听起来太令人困惑,但请帮忙!谢谢

最佳答案

我猜您知道如何将图像放入 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 的单元格中

关于swift - segue 将数据从一个 tableviewcontroller 解析到另一个 tableviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com