pmoney13
pmoney13

Reputation: 1115

Declaring my CellClass to extend to UITableViewCell?

I'm simply trying to display a tableview of names in the array of Smiles that I have set up. I need to declare my cell class "ClubCell" to extend to UITableViewCell, how would I go about doing this?

class  SmileClub: UITableViewController {

var Smiles: [String] = ["Price Garrett", "Michael Bishop", "Tom Kollross", "Cody Crawford", "Ethan Bernath", "Alex Mlynarz", "Ryan Murphy", "Kelly Murphy", "Ryan Roshan", "Sean Ko"]


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> ClubCell
{
    let cell: ClubCell = tableView.dequeueReusableCellWithIdentifier("ClubCell") as! ClubCell!
    cell.Name.text = self.Smiles[indexPath.row] as String
    return cell
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.Smiles.count
}

Upvotes: 0

Views: 1058

Answers (2)

Lamour
Lamour

Reputation: 3040

Your ClubCell is a subclass of UITableViewCell and it should look like:

class ClubCell:UITableViewCell
{
    @IBOutlet weak var Name:UILabel!
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
  let cell = tableView.dequeueReusableCellWithIdentifier("ClubCell",forIndexPath:indexPath) as! ClubCell
     cell.Name.text = self.Smiles[indexPath.row] 
      return cell
}

In this line cell.Name.text = self.Smiles[indexPath.row] as String no need to downcast to String because your static array is already into String so change it to cell.Name.text = self.Smiles[indexPath.row]

Upvotes: 0

rmaddy
rmaddy

Reputation: 318934

The return value for the cellForRowAtIndexPath method needs to be UITableViewCell, not ClubCell.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: ClubCell = tableView.dequeueReusableCellWithIdentifier("ClubCell") as! ClubCell!
    cell.Name.text = self.Smiles[indexPath.row] as String
    return cell
}

And make sure that your ClubCell class extends UITableViewCell.

It should be:

class ClubCell : UITableViewCell

Upvotes: 2

Related Questions