2016-06-25 25 views
-1

を「タイプUITableViewCel.typeの不変式に代入することはできません」。このエラーを修正するにはどうすればよいですか?エラー:</p> <blockquote> <p>Cannot assign to immutable expression of type <code>UITableViewCel.type</code></p> </blockquote> <p>私はto-doリストアプリを作成しようとしている:私はエラーを取得しています

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 
    } 

} 

答えて

0

1つの変数に対して2つの代入文を1行に含めることはできません。

代わりに、使用:関数dequeueReusableCellWithIdentifierはすでにデフォルトでのUITableViewCellを返すよう

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のように、細胞をキャストする必要はありません。

+0

私はちょうどそれらの2つのラインを切り替えると、それは動作しますか? – alexbomnd

+0

@alexbomndはあなたのために働いたのですか? –

0

UITableViewCellcellに割り当てようとしているというエラーが原因です。

var cell = UITableViewCell = ... 

そして、それ以外の任意のセルを返すようごdequeueReusableCellWithIdentifier(_:forIndexPath:)を使用する必要があるセルをデキューします。

関連する問題

 関連する問題