Reputation: 88
My error is in the first code below, in the case 2 of the switch.
cell.pointsNumber.text = "toto"
Xcode error : Value of type 'UITableViewCell' has no member 'pointsNumber'
I can't access to my class PerformancesViewCell
to fill the labels.
My cast doesn't work, my cell is still like a UITableViewCell
rather than a PerformancesViewCell
.
Thank you in advance for your help ;)
Cell Identifier:
let thirdCellIdentifier = "thirdCell"
TableView:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
switch indexPath.row
{
case 0:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(firstCellIdentifier, forIndexPath: indexPath) as UITableViewCell
cell.backgroundView = nil
cell.backgroundColor = UIColor.clearColor()
break;
case 1:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(secondCellIdentifier, forIndexPath: indexPath) as! DivisionsViewCell
break;
case 2:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(thirdCellIdentifier, forIndexPath: indexPath) as! PerformancesViewCell
cell.pointsNumber.text = "toto"
case 3:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(fourthCellIdentifier, forIndexPath: indexPath) as! ChampionsViewCell
break;
case 4:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(fifthCellIdentifier, forIndexPath: indexPath) as UITableViewCell
break;
default:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(fifthCellIdentifier, forIndexPath: indexPath) as UITableViewCell
break;
}
return cell
}
CustomCell:
import UIKit
class PerformancesViewCell: UITableViewCell
{
@IBOutlet weak var pointsNumber: UILabel!
@IBOutlet weak var challengesSucceed: UILabel!
@IBOutlet weak var bestPosition: UILabel!
@IBOutlet weak var averagePosition: UILabel!
@IBOutlet weak var challengeCreation: UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
Upvotes: 3
Views: 21048
Reputation: 15
This problem is not due to your code , happened with me due to changing classes name of view controller.
Xcode is not smart enough yet so, the solution is to delete the view controller that's crashing delete its class and create a new view controller and add a new file to it!!
took me days to get it fixed.
Upvotes: -1
Reputation: 107191
The issue is because of this line:
var cell: UITableViewCell!
You declared cell as type of UITableViewCell . So as the error states the UITableViewCell
has no member named pointsNumber
.
Change that line:
var pCell = tableView.dequeueReusableCellWithIdentifier(thirdCellIdentifier, forIndexPath: indexPath) as! PerformancesViewCell
pCell.pointsNumber.text = "toto"
Since you are using different type of cells and using a switch to distinguish between them, you need to assign the newly created cell(pCell) back to cell in the switch case itself.
cell = pCell
Upvotes: 11
Reputation: 6885
I agree with @Midhun MP 's answer.Generally,you should configure your custom tableviewcell class ,and return them in each case.But if you want return a cell outside of the switch func ,you can do this trick:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell: UITableViewCell!
switch indexPath.row
{
case 0:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(firstCellIdentifier, forIndexPath: indexPath) as UITableViewCell
cell.backgroundView = nil
cell.backgroundColor = UIColor.clearColor()
break;
case 1:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(secondCellIdentifier, forIndexPath: indexPath) as! DivisionsViewCell
break;
case 2:
let performancesViewCell = tableViewProfil.dequeueReusableCellWithIdentifier(thirdCellIdentifier, forIndexPath: indexPath) as! PerformancesViewCell
performancesViewCell.pointsNumber.text = "toto"
cell = performancesViewCell
case 3:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(fourthCellIdentifier, forIndexPath: indexPath) as! ChampionsViewCell
break;
case 4:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(fifthCellIdentifier, forIndexPath: indexPath) as UITableViewCell
break;
default:
cell = tableViewProfil.dequeueReusableCellWithIdentifier(fifthCellIdentifier, forIndexPath: indexPath) as UITableViewCell
break;
}
return cell
}
Upvotes: 1
Reputation: 3513
I would recommend removing the var
declaration of cell
and instead use
if indexPath.row==0 {
let cell:MyCustomTVC=self.myTableView.dequeueReusableCellWithIdentifier("myCustomTVC") as! MyCustomTVC
cell.myCustomLabel.text="asdf"
return cell
}
for each case. Just swap out the correct class and identifier for each.
Also, make sure each custom cell type is defined in your storyboard using the prototype cells in the Attribute Inspector and all cells have an identifier
Upvotes: 0