2017-12-22 10 views
0

두 가지 다른 테스트 케이스가 있습니다. 경우 1에서 이것은 CoreData에서 전체 항목을 인쇄하는 데 사용됩니다. app2에서 똑같은 일을하려했지만 작동하지 않습니다. 모든 핵심 데이터 항목이 포함 된 개별 셀에 모두 넣기 만하면됩니다.Tableview 셀에서 CoreData 인쇄

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

'문자열'을 생성해야하는'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! 
} 

주 :이 그냥 의사가 당신은 그대로 당신은 cellForRowAt 위임 다시 반복하지 않아도이

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

좋아하세요 잘못

을하고 오류를

0

을 포함 할 수있다 자체 반복 델타.

안전한 코딩을 위해 값을 래핑하지 않으려면 guard let 또는 if let을 사용하십시오. 언 랩핑을 강제하면 언제든지 앱이 중단 될 수 있습니다.

관련 문제