code-8
code-8

Reputation: 58662

Error while trying to add a custom cell into table view

I'm trying to add a custom cell into my tableview

This what I configured for custom CustomProfileView.xib file

enter image description here

Main.storyboard

enter image description here

I kept getting

Use of undeclared type 'CustomProfileCell'

This my entire code

//
//  FirstViewController.swift
//  tabbed
//
//  Created by Ben Heng on 7/18/18.
//  Copyright © 2018 LR Web Design. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var profileTableView: UITableView!

    var profileArray = [Profile]()

    override func viewDidLoad() {
        super.viewDidLoad()

        profileTableView.delegate = self
        profileTableView.dataSource = self
        profileTableView.register(UINib(nibName: "ProfileCell",bundle: nil) , forCellReuseIdentifier: "CustomProfileCell")

        retrieveProfiles()

    }



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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileCell

        cell.profileImage.image = UIImage(named: profileArray[indexPath.row].profileImage)
        cell.profileName.text = profileArray[indexPath.row].name
        cell.deviceCount.text = profileArray[indexPath.row].deviceCount

        return (cell)
    }



    func retrieveProfiles() {

        let p1 = Profile()
        p1.name = "John"
        p1.deviceCount = "10"
        p1.profileImage = "john"
        profileArray.append(p1)

        let p2 = Profile()
        p2.name = "Jane"
        p2.deviceCount = "5"
        p2.profileImage = "jane"
        profileArray.append(p2)

        let p3 = Profile()
        p3.name = "Andrew"
        p3.deviceCount = "4"
        p3.profileImage = "andrew"
        profileArray.append(p3)

        self.profileTableView.reloadData()


    }

}

Upvotes: 0

Views: 43

Answers (2)

Vlad Z.
Vlad Z.

Reputation: 3451

Please read this first, it explains how reuseIdentifier work and what it means.

First of all your nib name is CustomProfileViewCell, but you're registering nib with ProfileCell name.

Second error is that you're using ProfileCell identifier in storyboard, that should be same as your defined identifier which is - customProfileCell.

Your cell registration should looks like this

profileTableView.register(UINib(nibName: "CustomProfileViewCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")

and then you want to use it as here:

let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

According to your swift the name CustomProfileViewCell

let cell = tableView.dequeueReusableCell(withIdentifier: "customProfileCell", for: indexPath) as! CustomProfileViewCell

//

profileTableView.register(UINib(nibName: "CustomProfileViewCell",bundle: nil) , forCellReuseIdentifier: "customProfileCell")

Upvotes: 2

Related Questions