I have 2 different test cases here. In case 1 this is what I used to print the entire amount of entries from CoreData and it works. I have tried to do the exact same thing in app2 but it does not work. All I want to do is in each individual cell with all of the core data entries.
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!
}
if you want to print all the user entry in each cell then you can try below solution,
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!
}
and if you want to print each entry in each cell the try below solution,
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!
}
Note: This is just a pseudo might contain error
You are doing wrong
Do like this
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!
}
You don't need iterate again in cellForRowAt
delegate as it is self iterated delgate.
For safe coding use guard let
or if let
for unwrapping values. Force unwrap leads to app crash in any instance of time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.