2016-10-24 9 views
0

私はちょうどSwiftを開始しましたが、問題を抱えています。私はタグにラベルを付け、UIViewController、UiTableViewDelegateおよびUITableViewDataSourceを実装しました。私は、ストーリーボードの委任とのtableViewのデータソースに追加しましたが、私はこのエラーThread1:EXC_BAD_instruction(code = exc_i386_invop、subcode = 0 * 0)(tableViewプロジェクトの場合)

スレッド1得続ける:lblNameてみましょう上のtableViewプロジェクト にEXC_BAD_INSTRUCTION(コード= exc_i386_invop、サブコード= 0 * 0):UILabel = cell.viewWithTag(102)として! UILabel

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell")! 

    //remplissage cell 

    switch indexPath.section { 
    case 0: 
     let lblName:UILabel = cell.viewWithTag(102) as! UILabel 
     lblName.text = "30/09/2200" 

     let lblDate:UILabel = cell.viewWithTag(103) as! UILabel 
     lblDate.text = "30/09/2200" 

     //let imgProfile:UIImageView = cell.viewWithTag(103) as! UIImageView 
     // imgProfile.image = UIImage(named: "pexel") 

    cell.textLabel?.text = "Firas" 
    case 1: 
     cell.textLabel?.text = "sqdqsd" 
    default: 
     cell.textLabel?.text = "Nothing" 
    } 



    return cell 


} 
+0

あなたのストーリーボードにタグを設定しましたか? – zombie

+1

あなたの 'cell'は実際にそのタグを持つサブビューを持っていて、それはUILabelですか?これらのステートメントの1つは確かに偽です。 – alexburtnik

+0

あなたはUITableViewCellを使用しているので、なぜcell.detailTextLabelやcell.textLabelのようなラベルにアクセスできないのですか? – zombie

答えて

0
class FirasCell: UITableViewCell { 
    @IBOutlet weak var name: UILabel! 
    @IBOutlet weak var date: UILabel! 
} 

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

    // This will be an an option, as dequeue returns an optional 
    // This also assumes that all your cells are FirasCells 
     let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as?  FirasCell 

    // Sure you aren't thinking of rows here? An array would be rows, an array of arrays would be sections with rows in them 
    switch indexPath.section { 
    case 0: 
     cell?.name.text = "30/09/2200" 
     cell?.date.text = "30/09/2200" 

     cell?.textLabel?.text = "Firas" 
    case 1: 
     cell?.textLabel?.text = "Second Cell" 
    default: 
     cell?.textLabel?.text = "The rest of them" 
    } 

    // Quick and dirty check for this example as you are not allowed to return nil 
    return cell ?? UITableViewCell() 
} 

タグ・ソリューションを把握することは可能かもしれないが、それは非常に信頼性がありません。すばやく簡単な解決策は、自分のセルを作成して(上記のFirasCellを参照)、ストーリーボードのテーブル内のセルがそのタイプであることを確認することです。次に、2つのラベルをアウトレットとして追加します。

セルはFirasCellになり、デキューした後で直接テキストを設定できます。私はあなたのアプリを完全にクラッシュさせるので、例コードで!を使用することは避けています。したがって、あなたが非常に確信している場合にのみ!を使用してください。

最も面倒な部分は、おそらくアウトレットを設定しているかもしれませんが、それが簡単であることが分かったら、私よりもはるかに優れたチュートリアルがありますが、短いバージョンでは、接続インスペクタ(右のユーティリティバーの右のタブにある矢印が付いた円)を開いて、セルを選択し、コンセントの隣のサークルからUILabelへドラッグをクリックします。

関連する問題