今回の内容

コード
- 順位と画像URLとレシピタイトルを取得してきます。
Model
AlamofireDataModel
struct AlamofireDataModel{
let rank:String?
let mediumImageUrl:String?
let recipeTitle:String?
}
Alamofire
import Alamofire
import SwiftyJSON
class Alamofire{
private var afResultDataArray = [AlamofireDataModel]()
private var recipeMaterialContents:[String]?
}
extension Alamofire{
public func getAPIDatas(compltion: @escaping ([AlamofireDataModel]?,Error?) -> Void){
let apiKey = "作成したAPIKey"
AF.request(apiKey, method: .get, parameters: nil, encoding: JSONEncoding.default).response {[self] (response) in
switch response.result{
case .success:
afResultDataArray = []
let data = JSON(response.data as Any)
for getCount in 0..<data["result"].count{
if (data["result"][getCount]["rank"].string! != "" &&
data["result"][getCount]["mediumImageUrl"].string != "" &&
data["result"][getCount]["recipeTitle"].string != "") == true{
afResultDataArray.append(AlamofireDataModel(rank: data["result"][getCount]["rank"].string,
mediumImageUrl: data["result"][getCount]["mediumImageUrl"].string,
recipeTitle: data["result"][getCount]["recipeTitle"].string))
}
}
return compltion(afResultDataArray,nil)
case .failure(let error):
return compltion(nil, error)
}
}
}
}
View
TableViewCustomCell
import UIKit
class TableViewCustomCell: UITableViewCell {
@IBOutlet weak var rankLabel: UILabel!
@IBOutlet weak var mediumImageView: UIImageView!
@IBOutlet weak var recipeTitleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func prepareForReuse() {
super.prepareForReuse()
rankLabel.text = ""
mediumImageView.image = UIImage(named: "")
recipeTitleLabel.text = ""
}
}
Indicator
import UIKit
class Indicator{
public let indicatorView = UIActivityIndicatorView()
public func showIndicator(targetView:UIView){
indicatorView.center = targetView.center
indicatorView.style = .large
indicatorView.color = .systemRed
targetView.addSubview(indicatorView)
indicatorView.startAnimating()
}
}
Controller
ViewController
import UIKit
import SDWebImage
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var showRankingButton: UIButton!
private let alamofire = Alamofire()
private let indicator = Indicator()
private var cellContentsArray = [AlamofireDataModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TableViewCustomCell", bundle: nil), forCellReuseIdentifier: "Cell")
tableView.dataSource = self
tableView.delegate = self
}
@IBAction func showRanking(_ sender: UIButton) {
indicator.showIndicator(targetView: self.view)
alamofire.getAPIDatas { getData, error in
if error != nil{
return
}
guard let data = getData else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {[self] in
cellContentsArray = []
cellContentsArray = data
indicator.indicatorView.stopAnimating()
tableView.reloadData()
}
}
}
}
extension ViewController:UITableViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.height * 0.5
}
}
extension ViewController:UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(cellContentsArray.count)
return cellContentsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCustomCell
cell.rankLabel.text = "\(cellContentsArray[indexPath.row].rank ?? "読み込みERROR")位"
cell.mediumImageView.sd_setImage(with: URL(string: cellContentsArray[indexPath.row].mediumImageUrl!), completed: nil)
cell.recipeTitleLabel.text = cellContentsArray[indexPath.row].recipeTitle
return cell
}
}
終わり
ご指摘、ご質問などありましたら、コメントまでお願い致します。