Tableview accessories don't load correctly
up vote
2
down vote
favorite
I am trying to display a checkmark next to favourited reports in my project. I save the title into Core Data successfully and fetch them successfully too. I load them into an array called favourite
. I then compare against the title loaded into the cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as? CellClass else { return UITableViewCell()}
cell.titleLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
if (self.favourite.count > 0)
{
for i in 0...self.favourite.count - 1
{
if (objArray[indexPath.section].sectionObj?[indexPath.row].title == favourite[i].title!)
{
cell.accessoryType = .checkmark
}
}
}
return cell
}
Currently, I only have one piece of data in Core Data so one checkmark should be shown but it seems there is a recursive pattern of every 10 cells in my table view.
swift uitableview
add a comment |
up vote
2
down vote
favorite
I am trying to display a checkmark next to favourited reports in my project. I save the title into Core Data successfully and fetch them successfully too. I load them into an array called favourite
. I then compare against the title loaded into the cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as? CellClass else { return UITableViewCell()}
cell.titleLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
if (self.favourite.count > 0)
{
for i in 0...self.favourite.count - 1
{
if (objArray[indexPath.section].sectionObj?[indexPath.row].title == favourite[i].title!)
{
cell.accessoryType = .checkmark
}
}
}
return cell
}
Currently, I only have one piece of data in Core Data so one checkmark should be shown but it seems there is a recursive pattern of every 10 cells in my table view.
swift uitableview
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to display a checkmark next to favourited reports in my project. I save the title into Core Data successfully and fetch them successfully too. I load them into an array called favourite
. I then compare against the title loaded into the cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as? CellClass else { return UITableViewCell()}
cell.titleLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
if (self.favourite.count > 0)
{
for i in 0...self.favourite.count - 1
{
if (objArray[indexPath.section].sectionObj?[indexPath.row].title == favourite[i].title!)
{
cell.accessoryType = .checkmark
}
}
}
return cell
}
Currently, I only have one piece of data in Core Data so one checkmark should be shown but it seems there is a recursive pattern of every 10 cells in my table view.
swift uitableview
I am trying to display a checkmark next to favourited reports in my project. I save the title into Core Data successfully and fetch them successfully too. I load them into an array called favourite
. I then compare against the title loaded into the cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as? CellClass else { return UITableViewCell()}
cell.titleLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
if (self.favourite.count > 0)
{
for i in 0...self.favourite.count - 1
{
if (objArray[indexPath.section].sectionObj?[indexPath.row].title == favourite[i].title!)
{
cell.accessoryType = .checkmark
}
}
}
return cell
}
Currently, I only have one piece of data in Core Data so one checkmark should be shown but it seems there is a recursive pattern of every 10 cells in my table view.
swift uitableview
swift uitableview
asked Nov 12 at 1:24
Luke Varty
396
396
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Cells get reused. Whenever you conditionally set a property of a cell, you need to reset that property in other cases.
The simplest solution is to set the accessoryType
to .none
before the loop (and before the if
).
I also suggest optimizing the title a bit. You call objArray[indexPath.section].sectionObj?[indexPath.row].title
many times in this code. Do it once.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as! CellClass
let title = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.titleLbl.text = title
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
cell.accessoryType = .none
for favorite in self.favourite {
if title == favourite.title {
cell.accessoryType = .checkmark
break // no need to keep looking
}
}
return cell
}
I've shown lots of other code cleanup as well.
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Cells get reused. Whenever you conditionally set a property of a cell, you need to reset that property in other cases.
The simplest solution is to set the accessoryType
to .none
before the loop (and before the if
).
I also suggest optimizing the title a bit. You call objArray[indexPath.section].sectionObj?[indexPath.row].title
many times in this code. Do it once.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as! CellClass
let title = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.titleLbl.text = title
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
cell.accessoryType = .none
for favorite in self.favourite {
if title == favourite.title {
cell.accessoryType = .checkmark
break // no need to keep looking
}
}
return cell
}
I've shown lots of other code cleanup as well.
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
add a comment |
up vote
2
down vote
accepted
Cells get reused. Whenever you conditionally set a property of a cell, you need to reset that property in other cases.
The simplest solution is to set the accessoryType
to .none
before the loop (and before the if
).
I also suggest optimizing the title a bit. You call objArray[indexPath.section].sectionObj?[indexPath.row].title
many times in this code. Do it once.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as! CellClass
let title = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.titleLbl.text = title
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
cell.accessoryType = .none
for favorite in self.favourite {
if title == favourite.title {
cell.accessoryType = .checkmark
break // no need to keep looking
}
}
return cell
}
I've shown lots of other code cleanup as well.
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Cells get reused. Whenever you conditionally set a property of a cell, you need to reset that property in other cases.
The simplest solution is to set the accessoryType
to .none
before the loop (and before the if
).
I also suggest optimizing the title a bit. You call objArray[indexPath.section].sectionObj?[indexPath.row].title
many times in this code. Do it once.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as! CellClass
let title = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.titleLbl.text = title
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
cell.accessoryType = .none
for favorite in self.favourite {
if title == favourite.title {
cell.accessoryType = .checkmark
break // no need to keep looking
}
}
return cell
}
I've shown lots of other code cleanup as well.
Cells get reused. Whenever you conditionally set a property of a cell, you need to reset that property in other cases.
The simplest solution is to set the accessoryType
to .none
before the loop (and before the if
).
I also suggest optimizing the title a bit. You call objArray[indexPath.section].sectionObj?[indexPath.row].title
many times in this code. Do it once.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as! CellClass
let title = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.titleLbl.text = title
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"
cell.accessoryType = .none
for favorite in self.favourite {
if title == favourite.title {
cell.accessoryType = .checkmark
break // no need to keep looking
}
}
return cell
}
I've shown lots of other code cleanup as well.
answered Nov 12 at 1:26
rmaddy
236k27308374
236k27308374
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
add a comment |
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
Thank you for this comment! This has been really helpful!
– Luke Varty
Nov 12 at 3:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254915%2ftableview-accessories-dont-load-correctly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown