【问题标题】:'(String) -> AnyObject?' is not convertible to 'ResultCell'​'(字符串)-> AnyObject?不可转换为“ResultCell”​
【发布时间】:2015-04-07 12:40:42
【问题描述】:

我正在尝试投射我自己的自定义单元格,但似乎无法正常工作,我是 IOS 和 swift dev 的新手...从教程中我正在观看它工作正常,但是当我尝试时,它给了我一个错误.
这是代码。

class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var resultTable: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    let height = view.frame.height
    let width = view.frame.width

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 120
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell : ResultCell = UITableView.dequeueReusableCellWithIdentifier("userCell") as ResultCell

    return cell
}

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

}
// Deactivate the return button 
override func viewWillAppear(animated: Bool) {
    self.navigationItem.hidesBackButton = true
}


 }

这是我创建的单元格

  class ResultCell: UITableViewCell {

@IBOutlet weak var ContactprofilePic: UIImageView!
@IBOutlet weak var contactUserName: UILabel!
@IBOutlet weak var contactStatus: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let aWidth = UIScreen.mainScreen().bounds.width
    contentView.frame = CGRectMake(0, 0, aWidth, 120)

    ContactprofilePic.center = CGPointMake(60, 60)
    ContactprofilePic.layer.cornerRadius = ContactprofilePic.frame.size.width/2
    ContactprofilePic.clipsToBounds = true

    contactUserName.center = CGPointMake(230, 55)
}

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

    // Configure the view for the selected state
}

  }

但是我收到了这个错误

(字符串)-> AnyObject?不能转换为“ResultCell”

【问题讨论】:

  • 是编译时错误还是运行时错误?

标签: ios uitableview swift xcode6


【解决方案1】:
UITableView.dequeueReusableCellWithIdentifier(...)

应该是

tableView.dequeueReusableCellWithIdentifier(...)

dequeueReusableCellWithIdentifier() 是实例方法,必须 在作为第一个传递的 tableView 实例上调用 cellForRowAtIndexPath 方法的参数。


其实表达式

UITableView.dequeueReusableCellWithIdentifier

有效,如http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/ 中所述。这个表达式的值是 类型的“咖喱函数”

(UITableView) -> (String) -> AnyObject?

这很可能不是您打算做的,但解释了 错误提示

(字符串)-> AnyObject?不能转换为“ResultCell”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多