2017-12-22 40 views
0

ここには2つの異なるテストケースがあります。ケース1の場合、これはCoreDataからのエントリの全量を印刷するために使用したもので、動作します。私はapp2で全く同じことをしようとしましたが、うまくいきません。私がしたいのは、すべてのコアデータエントリを持つ個々のセルです。Coreviewをテーブルビューのセルに印刷

APP1

override func viewDidLoad() { 
    super.viewDidLoad() 
    user = coreDataHandler.getSortedData() 

    for i in user! { 
     displayL.text = String(i.username) 
    } 
} 

APP2

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    user = coreDataHandler.getSortedData() 

    return user!.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = cctv.dequeueReusableCell(withIdentifier: "cell") 

    for i in user! { 
     cell?.textLabel?.text = String(describing: i.username) 
     return cell! 
    } 

    return cell! 
} 
+0

「String」を作成するために必要な 'username'のタイプは何ですか?そして、決して* numberOfRowsInSection'にデータ*を取得しません。 – vadian

答えて

0

あなたは、各セル内のすべてのユーザーエントリを印刷したい場合は、あなたが

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    user = coreDataHandler.getSortedData() 

    return user!.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

    for i in user! { 
     cell?.textLabel?.text = String(describing: i.username) 
    } 

    return cell! 
} 

としたい場合は、解決策の下に試すことができます各セルの各エントリを印刷するには、以下の解決方法を試してください。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    user = coreDataHandler.getSortedData() 

    return user!.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

    let currentUser = user[indexPath.row] 

    cell?.textLabel?.text = String(describing: currentUser.username) 

    return cell! 
} 

注:これはちょうど擬似をそのままこの

let user: [UserType] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    user = coreDataHandler.getSortedData() 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return user.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
    let currentUser = user[indexPath.row] 
    cell?.textLabel?.text = "\(currentUser.username)" 
    return cell! 
} 

のようにあなたがcellForRowAtデリゲートで再び反復処理を必要としないでくださいエラー

0

あなたは間違って

を行っているが含まれているかもしれないです自己反復する。

安全なコーディングのために、アンラッピング値としてguard letまたはif letを使用してください。 強制的にアンラップすると、時間の経過とともにアプリがクラッシュする可能性があります。

関連する問題