Swift:将数据从 tableView 显示到另一个 ViewController(JSON、Alamorife、AlamofireImage)
Posted
技术标签:
【中文标题】Swift:将数据从 tableView 显示到另一个 ViewController(JSON、Alamorife、AlamofireImage)【英文标题】:Swift: Show data from tableView to another ViewController (JSON, Alamorife, AlamofireImage) 【发布时间】:2018-06-26 06:53:44 【问题描述】:我正在尝试做一个从 JSON 获取数据的应用程序。
在下图中您可以看到该项目: Project 如果我们点击照片会打开详细信息页面。问题是因为我不知道如何获取详细信息页面中显示的数据。请帮我。 这是代码
import UIKit
import Alamofire
import AlamofireImage
import SwiftyJSON
class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate
@IBOutlet weak var searchbarValue: UISearchBar!
weak open var delegate: UISearchBarDelegate?
@IBOutlet weak var tableView: UITableView!
var albumArray = [AnyObject]()
var url = ("https://jsonplaceholder.typicode.com/photos")
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.searchbarValue?.delegate = self
Alamofire.request("https://jsonplaceholder.typicode.com/photos").responseJSON (responseData) -> Void in
if((responseData.result.value) != nil)
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar[].arrayObject
self.albumArray = resData as [AnyObject]; ()
if self.albumArray.count > 0
self.tableView.reloadData()
public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) // called when text ends editing
callAlamo(searchTerm: searchbarValue.text!)
func callAlamo(searchTerm: String)
Alamofire.request("https://jsonplaceholder.typicode.com/photos").responseJSON (responseData) -> Void in
if((responseData.result.value) != nil)
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar[].arrayObject
self.albumArray = resData as [AnyObject]; ()
if self.albumArray.count > 0
self.tableView.reloadData()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return albumArray.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CostumTableViewCell
let title = albumArray[indexPath.row]
cell?.titleLabel?.text = title["title"] as? String
//cell?.url?.image = UIImage(data: title as! Data)
let imageUrl = title["thumbnailUrl"] as? String
//print(imageUrl)
let urlRequest = URLRequest(url: URL(string: imageUrl!)!)
Alamofire.request(urlRequest).responseImage response in
if let image = response.result.value
// print("image downloaded: \(title["url"])")
cell?.url?.image = image
return cell!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
performSegue(withIdentifier: "showDetails", sender: self)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
let indexPath = self.tableView.indexPathForSelectedRow?.row
let vc = segue.destination as! DetailsViewController
//here should be the code
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
你还可以看到DetailsViewController代码:
import UIKit
class DetailsViewController: UIViewController
var image2 = UIImage()
var title2 = String()
@IBOutlet var mainImageView: UIImageView!
@IBOutlet var songTitle: UILabel!
override func viewDidLoad()
songTitle.text = title2
mainImageView.image = image2
【问题讨论】:
【参考方案1】:您可以使用以下代码轻松地将值从表视图传递到详细视图:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
let indexPath = self.tableView.indexPathForSelectedRow
let cell : CostumTableViewCell = self.tableView.cellForRow(at: indexPath!) as! CostumTableViewCell
let vc = segue.destination as! DetailsViewController
vc.image2 = cell.url.image!
vc.title2 = cell.titleLabel.text!
【讨论】:
【参考方案2】:class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate
var customArr = [CustomElement]()
var arr = [Any]()
// In viewDidLoad , you can append element to customArr
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
var image = customArr[indexpath.row].image
var title = customArr[indexpath.row].title
arr.append(image)
arr.append(title)
performSegue(withIdentifier: "showDetails", sender: arr)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
let indexPath = self.tableView.indexPathForSelectedRow?.row
if segue.identifier = "showDetails"
if let vc = segue.destination as! DetailsViewController
vc.arr = sender
//here should be the code
class DetailsViewController: UIViewController
var arr = [Any]()
【讨论】:
以上是关于Swift:将数据从 tableView 显示到另一个 ViewController(JSON、Alamorife、AlamofireImage)的主要内容,如果未能解决你的问题,请参考以下文章
在将数据从 tableView 传递到另一个 viewController Swift 时需要帮助。查看其他示例后继续出错
如何使用 SWrevaelviewController swift 3 将数据从 tableview 的特定行传递到另一个视图控制器
Swift 4.2 - 如何从 tableView 中的“didSelectRowAtIndexPath”方法调用“indexPath.row”到另一个类?
Swift-从另一个ViewController插入TableView中的项目