Das Mupp Das Mupp - 26 days ago 8
Swift Question

Value of type `UITableViewCell´ har no member `MyNote`

I get this error


Value of type
UITableViewCell
has no member
MyNote



on this row line:

cell.NyNote = cell.textLabel?.text


Dont know what to do please help me out!

import UIKit

class ViewController: UITableViewController {

let array = ["item1", "item2", "item3"]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.rowHeight = 70
tableView.backgroundView = UIImageView(image: UIImage(named: "concrete_seamless"))
}
// navigation bar animated
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

navigationController?.navigationBar.alpha = 0.5
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customcell")! as UITableViewCell

if(indexPath.item % 2 == 0){
cell.backgroundColor = UIColor.clear
}
else{
cell.backgroundColor = UIColor.white
UIColor.white.withAlphaComponent(0.2)

cell.textLabel?.backgroundColor = UIColor.white
UIColor.white.withAlphaComponent(0.0)

}
// text color
cell.textLabel?.textColor = UIColor.darkGray

cell.textLabel?.text = array[indexPath.item]
cell.NyNote = cell.textLabel?.text
return cell
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "detailview") {
let cell = sender as! customcell
let detailview = segue.destination as! DetailViewController
detailview.preMyNotes = cell.NyNote
}

}

}


And here is the var MyNotes:

import UIKit

class customcell: UITableViewCell {

var NyNote:String?

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
}

}

Answer

You have to cast the cell to your subclass, otherwise the compiler doesn't know about it. Change this line

let cell = tableView.dequeueReusableCell(withIdentifier: "customcell")! as UITableViewCell

to

let cell = tableView.dequeueReusableCell(withIdentifier: "customcell")! as customcell
Comments