Swift ios如何获取解析数据并将其分配给标签

时间:2018-04-06 07:52:12

标签: ios swift

我想知道如何从swift中解析的json响应中获取每个名称。我想根据我的代码library(dplyr) ndf <- df %>% group_by(subscriberid, variable, value, gender, resp, cna) %>% mutate(cwe = na_if(cwe, lag(cwe)), hw = na_if(hw, lead(hw))) %>% ungroup() 将响应的名称值分配给# A tibble: 6 x 8 subscriberid variable value gender cwe hw resp cna <int> <chr> <dbl> <dbl> <int> <int> <int> <int> 1 1177460837 3134 1. 2. NA NA NA 3 2 1177460837 4550 2. 2. 50 NA NA 1 3 1177460837 4550 2. 2. NA 48 NA 1 4 1146526049 5160 1. 1. NA NA NA 3 5 1146526049 2530 2. 2. 30 NA NA 1 6 1146526049 2530 2. 2. NA 26 NA 1 。标签将根据cell.label.text的数量自动化,我想要的是它会自动化,具体取决于json响应中有多少名称

cell.label.text = item.name

Http请求代码:

item.name

1 个答案:

答案 0 :(得分:1)

import UIKit
import Alamofire



class MenuCollectionViewController: UIViewController, 
UICollectionViewDelegate, UICollectionViewDataSource {

var titleArray = [String]()

@IBOutlet var collectionView: UICollectionView!
@IBAction func signOutButtonIsPressed(_ sender: Any) {
    let appDelegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.showLoginScreen()
}
@IBOutlet var signoutButton: UIButton!
var items = [Item]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.signoutButton.layer.cornerRadius = 3.0
    demoApi()
}

override func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(animated)

    self.navigationController?.navigationBar.isHidden = true
    self.navigationItem.hidesBackButton =  true

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return titleArray.count
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionCell
    cell.nameLabel.text = titleArray[indexPath.row]
    return cell
}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}

func demoApi() {
    Alamofire.request("https://jsonplaceholder.typicode.com/posts", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            guard let json = response.result.value as! [[String:Any]]? else{ return}
            print("Response \(json)")
            for item in json {

                if let title = item["title"] as? String {
                    self.titleArray.append(title)
                }

                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }
            }
            break

        case .failure(_):
            print("Error")
            break

        }
    }

}

 }


 class CollectionCell: UICollectionViewCell {

@IBOutlet weak var imgPhoto: UIImageView!
@IBOutlet weak var nameLabel: UILabel!


  }