我正在尝试为应用获取HomeFeed,因为这是我的第一个主要应用。
我发现很难获取JSON数据并在tableViewCell
上显示,特别是转换{ {1}}并使用它。
任何形式的帮助都会受到高度赞赏和生命保护!
这是我的JSON Array/Dictionary
课程:
HomeFeedTableView
我得到了这个JSON响应
class HomeFeedTableView: UIViewController {
@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet weak var cameraBtn: UIButton!
var tableView = UITableView()
var tableAuthor = [String]()
var tableTitle = [String]()
override func viewDidLoad() {
super.viewDidLoad()
//cameraBtn possition
cameraBtn.layer.zPosition = 1000
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
getJSON()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Get JSON
func getJSON(){
let getEndPoint: String = "http://myURL/api/get_user_post/"
Alamofire.request(getEndPoint)
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET")
print(response.result.error!)
return
}
if let value = response.result.value {
let json = JSON(value)
//print(json.description)
for items in json.dictionary! {
//let author: String? = anItem["author"].stringValue
//let title: String? = anItem["title"].stringValue
//self.tableAuthor.append(author!)
//self.tableTitle.append(title!)
print(items)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
// MARK: - Table view data source
func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return status.count
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell: HomeFeedTableViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeCell") as! HomeFeedTableViewCell
let img = UIImage(named: imageMain[(indexPath as NSIndexPath).row])
cell.ImageMain.image = img
// cell.statusMain.text = tableAuthor[indexPath.row]
let proImg = UIImage(named: imageMain[(indexPath as NSIndexPath).row])
cell.profileImage.image = proImg
cell.profileImage.layer.cornerRadius = 20.00
cell.profileImage.clipsToBounds = true
cell.likeBtn.layer.borderWidth = 1.2
cell.likeBtn.layer.borderColor = UIColor.gray.cgColor
cell.likeBtn.layer.cornerRadius = 5
cell.likeBtn.clipsToBounds = true
cell.commentBtn.layer.borderWidth = 1.2
cell.commentBtn.layer.borderColor = UIColor.gray.cgColor
cell.commentBtn.layer.cornerRadius = 5
cell.commentBtn.clipsToBounds = true
return cell
}
@IBAction func cameraBtnClicked(_ sender: AnyObject) {
print("Camera Clicked")
}
}
因为我是Alamofire的新手,所以对我来说这让我很困惑。请帮忙。
答案 0 :(得分:1)
您的JSON响应是Array
的类型而不是字典,所以这样设置为循环。
for items in json.arrayValue {
let author: String? = anItem["author"].stringValue
let title: String? = anItem["title"].stringValue
self.tableAuthor.append(author!)
self.tableTitle.append(title!)
}
注意:您需要创建自定义类型[Book]
,而不是使用多个数组,首先需要创建自定义类Book
。