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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 1:24









      Luke Varty

      396




      396






















          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.






          share|improve this answer




















          • Thank you for this comment! This has been really helpful!
            – Luke Varty
            Nov 12 at 3:41










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























          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.






          share|improve this answer




















          • Thank you for this comment! This has been really helpful!
            – Luke Varty
            Nov 12 at 3:41














          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.






          share|improve this answer




















          • Thank you for this comment! This has been really helpful!
            – Luke Varty
            Nov 12 at 3:41












          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 1:26









          rmaddy

          237k27309375




          237k27309375











          • 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




          Thank you for this comment! This has been really helpful!
          – Luke Varty
          Nov 12 at 3:41

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands