vendredi 14 août 2015

How to access custom cell created in xib

I have designed a custom cell in xib. And created a class for that as well. The code for that class is as given below-

class ProjectsCell {    
@IBOutlet var projectNameLabel: UILabel!  //This is outlet to which I will assign value.
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

Now I have a view controller there I am trying to access this cell. In storyboard I have given reusable identifier "Cell". Now I am using this cell like the following code-

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProjectsCell
        let project = projects[indexPath.row]
        cell.projectNameLabel?.text = project.ProjectName //********* Here I am getting exception for projectNameLabel. 
        return cell

I think that label is coming null. I have tried the following approach also but that is also not working.

var cell: ProjectsCell = tableView.dequeueReusableCellWithIdentifier("Cell") as ProjectsCell
        tableView.registerNib(UINib(nibName: "ProjectsCell", bundle: nil), forCellReuseIdentifier: "Cell")
        cell = tableView.dequeueReusableCellWithIdentifier("Cell") as ProjectsCell

What can be the issue if anyone has faced this same issue.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire