2016-10-28 9 views
-2

これはエラーです。私はこれが簡単な修正だと確信していますが、私はそれに少し問題があります。このスレッドを更新してコードを追加しました。"タイプ 'UITableViewCell'の値を 'UILabel'にキャストできませんでした。"

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return 5 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "CheckListItem", for: indexPath) 

    let label = cell.viewWithTag(1000) as! UILabel 

    switch indexPath.row { 
    case 0: 
     label.text = "Walk the dog" 
    case 1: 
     label.text = "Brush my teeth" 
    case 2: 
     label.text = "Learn iOS development" 
    case 3: 
     label.text = "Soccer practice" 
    case 4: 
     label.text = "Eat ice cream" 
    default: 
     label.text = "Nothing" 
    } 
    return cell 
} 

Screenshot

+2

ラベルまたはセルにタグを設定しましたか?また、本当にタグで作業するべきではありません。セルをサブクラス化し、セルサブクラスのラベルとプロパティの間にコンセントを設定します。 –

+1

画像としてコードを投稿しないでください。関連コードをコピーしてテキストとして質問に貼り付けてください。それは、読んだり、参照するのが簡単になります。 – rmaddy

+0

ありがとうございます。セルではなく、ラベルにタグを設定する必要がありました。 –

答えて

0

これはviewWithTag(_:)機能が何をするかである:何のサブビューは、そのタグを持っていない場合

は 特定のタグ、またはゼロで(自身を含む)ビューの最寄りの子孫を返します。

だから戻ってきたのはオブジェクトそのものとそれに最も近い子孫です。それはあなただけのセル内のテキストを設定しようとしているので、あなたは何ができるかが、ちょうどこのようにコードを変更しているように見えます:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "CheckListItem", for: indexPath) 

    switch indexPath.row { 
    case 0: 
     cell.textLabel.text = "Walk the dog" 
    case 1: 
     cell.textLabel.text = "Brush my teeth" 
    case 2: 
     cell.textLabel.text = "Learn iOS development" 
    case 3: 
     cell.textLabel.text = "Soccer practice" 
    case 4: 
     cell.textLabel.text = "Eat ice cream" 
    default: 
     cell.textLabel.text = "Nothing" 
    } 
    return cell 
} 
0

私はiOSの見習いの一連の作業とちょうど同じ問題などが発生しましたあなたはしました。

Main.storyboardに1000というタグを付ける場所を確認したいと思うかもしれません - この理由で失敗した場合は、おそらくラベルの代わりにUITableViewCellにタグを付けたでしょうコンテンツビューでUILabelと入力します)。

関連する問題