“UITableViewCell”类型的值没有成员`MyNote`

时间:2016-11-08 12:01:37

标签: swift

我收到此错误

  

UITableViewCell类型的值没有成员MyNote

在这一行上:

cell.NyNote = cell.textLabel?.text

不知道该怎么办请帮助我!

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
    }

  }

}

这是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
  }

}

1 个答案:

答案 0 :(得分:0)

您必须将单元格强制转换为子类,否则编译器不会知道它。改变这一行

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

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