0
我的tableView顯示來自網絡的圖像列表。我試圖將圖像數據傳遞給下一個控制器,但我失敗了。 這是我的步驟:Swift 2 TableView將圖像傳遞到網絡中的下一個控制器
1)聲明一個空的UIImage變量=> 2)設置單元格的圖像從url => 3)當用戶選擇一個單元格時,獲取單元格的圖像=>將它傳遞給下一個控制器
但是,它傳遞的是空的圖像到下一個控制器..
var myImage: UIImage?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! customCell
if let cellImage = URLArray[indexPath.row]["image"] {
do {
let imgURL: NSURL = NSURL(string: cellImage!)!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in
if (error == nil && data != nil) {
func display_image() {
cell.placeImage.image = UIImage(data: data!)!
}
dispatch_async(dispatch_get_main_queue(), display_image)
} else {
cell.placeImage.image = UIImage(named: "image1")!
}
}
task.resume()
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as! customCell
self.myImage = selectedCell.placeImage.image
performSegueWithIdentifier("showDetail", sender: self)
}
// prepareForSegue
if segue.identifier == "showDetail" {
let controller = (segue.destinationViewController as! secondViewController)
controller.Image = self.myImage
我已經設置了segue的標識符。它不工作...我不知道如何解決這個問題。 –
嘗試刪除「performSegueWithIdentifier(」showDetail「,sender:self)」,也許它與故事板衝突 – Ezechiele