Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement in Table with an API #34

Open
cbartellds opened this issue Jul 10, 2018 · 0 comments
Open

Implement in Table with an API #34

cbartellds opened this issue Jul 10, 2018 · 0 comments

Comments

@cbartellds
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant