2017-12-22 23 views
0

Ich habe hier zwei verschiedene Testfälle. Im Fall 1 habe ich die gesamte Anzahl von Einträgen aus CoreData ausgedruckt und es funktioniert. Ich habe versucht, genau dasselbe in app2 zu tun, aber es funktioniert nicht. Alles, was ich tun möchte, ist in jeder einzelnen Zelle mit allen Kerndateneinträgen.CoreData auf Tabellenansicht Zelle drucken

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

Welcher Art ist 'username', aus dem Sie eine' String' erstellen müssen? Und niemals * bekomme Daten in 'numberOfRowsInSection' – vadian

Antwort

0

, wenn Sie in jeder Zelle alle Benutzereintrag drucken möchten, dann können Sie unter Lösung versuchen,

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! 
} 

und wenn Sie möchten, um jeden Eintrag in jeder Zelle zu drucken, versuchen Sie die Lösung unter

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! 
} 

Hinweis: Dies ist nur ein Pseudofehler

0

Sie falsch machen

wie Sie dieses

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! 
} 

Sie brauchen nicht zu wiederholen wieder in cellForRowAt Delegierten enthalten könnte, wie es ist selbst wiederholtes delgate.

Zur sicheren Kodierung verwenden Sie guard let oder if let zum Auspacken von Werten. Force-Unwrap führt zu jedem Zeitpunkt zu einem Absturz der App.

Verwandte Themen