Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

ios - Swift 5 - Set TextLabel in Custom CollectionviewCell from JSON Array

I have created a custom cell for my collectionview that i have set in a tableview for my app. I need to know to to set the text label to appear as the items in my array that is listed in my JSON File that is local.

View Controller:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var workoutData = [Models]()
    
    
    @IBOutlet weak var tableview: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        parseJSON()
        
        tableview.register(CollectionTableViewCell.nib(), forCellReuseIdentifier: CollectionTableViewCell.identifier)
        
        print(workoutData)
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return workoutData.count
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return workoutData[section].title
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return workoutData[section].workouts.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: CollectionTableViewCell.identifier, for: indexPath) as! CollectionTableViewCell
        cell.configure(with: workoutData)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableview.deselectRow(at: indexPath, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 250.0
        
    }
    

    
    
    func parseJSON() {
        
        let url = Bundle.main.url(forResource: "data", withExtension: "json")!
        do{
            let data = try Data(contentsOf: url)
            workoutData = try JSONDecoder().decode([Models].self, from: data)
        } catch {
            print(error)
        }
        
        
    }


}

My Custom Cell File:

import UIKit

class MyCollectionViewCell: UICollectionViewCell {
    
    var workoutData = [Models]()
    
    

    @IBOutlet weak var myLabel: UILabel!
    
    static let identifier = "MyCollectionViewCell"
    
    static func nib() -> UINib {
        return UINib(nibName: "MyCollectionViewCell", bundle: nil)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    public func configure(with model: Models) {
            self.myLabel.text = //what to put here.
        print(model.title)


    }

}

My JSON File:

[
        {
            "title": "Chest",
            "workouts": [
                "Bench",
                "Pushup",
                "Incline Press",
                "Decline Press",
            ]
        },
        {
            "title": "Back",
            "workouts": [
                "Barbell Row",
                "Lat Pulldown",
                "Deadlift",
                "Back Extension",
            ]
        },
        {
            "title": "Arms",
            "workouts": [
                "Barbell Curl",
                "Dumbbell Curl",
                "Tricep Pressdown",
                "Skull Crusher",
            ]
        }
]

I want my text label to show the items in my workouts array. when i set it i get the error "cannot assign value of type '[String]' to type 'String'". I would appreciate any help or directions. Thanks

EDIT:

I am looking to build my layout similar to the horizontal scroll of the the app store

enter image description here

question from:https://stackoverflow.com/questions/65911496/swift-5-set-textlabel-in-custom-collectionviewcell-from-json-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

workouts is an array of String. So firstly you need to get the String from array by index.

You can use this to show the first value on the label

self.myLabel.text = models.workouts[0]

Or If you want to show all the workouts values on the array then you can use

self.myLabel.text = models.workouts.joined(separator: ", ")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...