Skip to content

Implement in Table with an API #34

@cbartellds

Description

@cbartellds

I am wanting to implement a table view, which is populated by an API, in the drop down. I added a table to the XIB file and created another XIB file for the table cell.

Primary View Controller:

class EcardSingleCategoryViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

var ecards = Content
let imageCache = NSCache<NSString,AnyObject>()

@IBOutlet weak var ecardCollection: UICollectionView!

var name = ""
var id = ""

override func viewDidLoad() {
    super.viewDidLoad()
    
    let ZBdropDownViews = Bundle.main.loadNibNamed("EcardCategories", owner: nil, options: nil) as? [UIView]
    let view = YNDropDownMenu(frame:CGRect(x: 0, y: 64, width: UIScreen.main.bounds.size.width, height: 64), dropDownViews: ZBdropDownViews!, dropDownViewTitles: ["Choose Category"])

   self.view.addSubview(view)
}

In the initViews() function, I get the following error ... for the "categoryTable.register( ... " code

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I'm not sure how I would go about calling the cell's XIB file and populate the data.

Drop Down code:

class EcardCategoriesMenu: YNDropDownView {

@IBOutlet var categoryTable: UITableView!

var categories = [CategoryItem]()

override init(frame: CGRect) {
    super.init(frame: frame)
    self.initViews()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    self.initViews()
}

func initViews() {
    
  //  categoryTable.register(UINib(nibName: "EcardCategoriesCell", bundle: nil), forCellReuseIdentifier: "EcardCategoriesCell")
    
    categoryTable.delegate = self
    categoryTable.dataSource = self
    
    DispatchQueue.main.async {
        let jsonUrlString = "https://*****/category"
        guard let url = URL(string: jsonUrlString) else { return }

        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else { return }
            
            if err == nil {
                do {
                    let decoder = JSONDecoder()
                    let ecardcategory = try decoder.decode(Category.self, from: data)
                    self.categories = ecardcategory.category
                    self.categories.sort(by: {$0.title < $1.title})
                } catch let err {
                    print("Err", err)
                }
                
                DispatchQueue.main.async {
                    print(self.categories.count)
                    self.categoryTable.reloadData()
                }
            }
        }.resume()
    }
}
}
extension EcardCategoriesMenu: UITableViewDelegate, UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return categories.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "EcardCategoriesCell", for: indexPath) as? EcardCategoriesCell else { return UITableViewCell() }
    cell.categoryName.text = ("\(categories[indexPath.row].title)")
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let mainStoryboard:UIStoryboard = UIStoryboard(name: "Ecard", bundle: nil)
    let desVC = mainStoryboard.instantiateViewController(withIdentifier: "EcardSingleCategoryViewController") as! EcardSingleCategoryViewController
    desVC.id = String(categories[indexPath.row].id)
    let navigationController = UIApplication.shared.keyWindow?.rootViewController as! UINavigationController
    navigationController.pushViewController(desVC, animated: true)
}
}

Cell Code:

class EcardCategoriesCell: UITableViewCell {
    @IBOutlet weak var categoryName: UILabel!
}

Any help sorting through this would be great.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions