错误:"无法分配给UITableViewCel.type"类型的不可变表达式;

时间:2016-06-25 00:30:42

标签: ios xcode swift uitableview

我收到错误:

  

无法分配类型UITableViewCel.type

的不可变表达式

我正在尝试创建一个待办事项列表应用。如何解决此错误?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   Error -> ** var cell = UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell**
    if  cell {
        cell = UITableView(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }
    cell.textLable.text = "Hi"
    return cell
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

}

2 个答案:

答案 0 :(得分:0)

对于一个变量,您不能在一行中有两个赋值语句。

相反,请使用:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCellWithIdentifier("Cell") {
        cell.textLabel?.text = "Hi" //check spelling of textLable?
        return cell
    } else {
        return UITableViewCell()
    }
}

您不需要将单元格转换为UITableViewCell,因为函数dequeueReusableCellWithIdentifier默认情况下已返回UITableViewCell。

答案 1 :(得分:0)

您的错误来自于您尝试将UITableViewCell分配给cell

var cell = UITableViewCell = ...

要使单元格出列,您应该使用dequeueReusableCellWithIdentifier(_:forIndexPath:),因为它返回一个非可选单元格。

相关问题