来自ITableViewCell的垂头丧气?到UITableViewCell只能打开选项

时间:2016-04-01 07:27:22

标签: ios xcode swift tableview

我该如何解决这个问题?谢谢  这是一个tableviewcell错误,我不明白这个问题。请帮助我。 我有一个像ITableViewCell的Downcast这样的错误? UITableViewCell只能打开选项,你的意思是使用'!'?

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

   var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell") as?UITableViewCell

    if(cell == nil)
    {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }
    let film = self.filmler[indexPath.row] as! Dictionary<String, AnyObject>
    let filmAdi = film["ad"]! as! String
    cell!.textLabel!.text = filmAdi

    return cell!;
}

3 个答案:

答案 0 :(得分:3)

    var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell")

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    }

答案 1 :(得分:0)

// you need to maintain one space between ? symbol and UITableViewCell as follows , it will work
var cell : UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell

答案 2 :(得分:0)

请在您的代码中添加此行

var cell:UITableViewCell?  = tableView.dequeueReusableCellWithIdentifier("cell")

dequeueReusableCellWithIdentifier返回可选类型UITableViewCell。所以你不需要输入强制转换

public func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell? 
相关问题