TableViewController selective cells gets deselected on table relaod












0















I have an issue where some cells are deselected when i try to reload the rows/table with refresh controller and scroll the tableview immediately. i checked it with the deselection in diSelectrow and in didDeselectRow.In both methods the debug counter isn't encountered.
i have the following code in cellforRow



if (isEditClicked == true || [indexarray containsObject:indexPath]) {
[cardinboxCardCell setSelectionStyle:UITableViewCellSelectionStyleDefault];
}


following in didselectrow



if (!self.tableView.isEditing) {
Card *card;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
card = [_fetchedResultsController objectAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"pushToMyCardDetails" sender:card];
}else {
selectedDeleteCardCount += 1;
if (deleBtn) {
[deleBtn setEnabled:true];
}
[indexarray addObject:indexPath];
NSString* cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
[self.navigationController.navigationBar.topItem setTitle:cardsCount];
}


following in didDeselctrow



if (self.tableView.isEditing) {
selectedDeleteCardCount -= 1;
NSString* cardsCount = [NSString stringWithFormat:@"Inbox"];
[indexarray removeObject:indexPath];
if (selectedDeleteCardCount != 0){
cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
if (deleBtn) {[deleBtn setEnabled:true];}
}else {
if (deleBtn) {[deleBtn setEnabled:false];}
}
[self.navigationController.navigationBar.topItem setTitle:cardsCount];
}


any clue will be helpful. thanks.










share|improve this question



























    0















    I have an issue where some cells are deselected when i try to reload the rows/table with refresh controller and scroll the tableview immediately. i checked it with the deselection in diSelectrow and in didDeselectRow.In both methods the debug counter isn't encountered.
    i have the following code in cellforRow



    if (isEditClicked == true || [indexarray containsObject:indexPath]) {
    [cardinboxCardCell setSelectionStyle:UITableViewCellSelectionStyleDefault];
    }


    following in didselectrow



    if (!self.tableView.isEditing) {
    Card *card;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    card = [_fetchedResultsController objectAtIndexPath:indexPath];
    [self performSegueWithIdentifier:@"pushToMyCardDetails" sender:card];
    }else {
    selectedDeleteCardCount += 1;
    if (deleBtn) {
    [deleBtn setEnabled:true];
    }
    [indexarray addObject:indexPath];
    NSString* cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
    [self.navigationController.navigationBar.topItem setTitle:cardsCount];
    }


    following in didDeselctrow



    if (self.tableView.isEditing) {
    selectedDeleteCardCount -= 1;
    NSString* cardsCount = [NSString stringWithFormat:@"Inbox"];
    [indexarray removeObject:indexPath];
    if (selectedDeleteCardCount != 0){
    cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
    if (deleBtn) {[deleBtn setEnabled:true];}
    }else {
    if (deleBtn) {[deleBtn setEnabled:false];}
    }
    [self.navigationController.navigationBar.topItem setTitle:cardsCount];
    }


    any clue will be helpful. thanks.










    share|improve this question

























      0












      0








      0








      I have an issue where some cells are deselected when i try to reload the rows/table with refresh controller and scroll the tableview immediately. i checked it with the deselection in diSelectrow and in didDeselectRow.In both methods the debug counter isn't encountered.
      i have the following code in cellforRow



      if (isEditClicked == true || [indexarray containsObject:indexPath]) {
      [cardinboxCardCell setSelectionStyle:UITableViewCellSelectionStyleDefault];
      }


      following in didselectrow



      if (!self.tableView.isEditing) {
      Card *card;
      [tableView deselectRowAtIndexPath:indexPath animated:YES];
      card = [_fetchedResultsController objectAtIndexPath:indexPath];
      [self performSegueWithIdentifier:@"pushToMyCardDetails" sender:card];
      }else {
      selectedDeleteCardCount += 1;
      if (deleBtn) {
      [deleBtn setEnabled:true];
      }
      [indexarray addObject:indexPath];
      NSString* cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
      [self.navigationController.navigationBar.topItem setTitle:cardsCount];
      }


      following in didDeselctrow



      if (self.tableView.isEditing) {
      selectedDeleteCardCount -= 1;
      NSString* cardsCount = [NSString stringWithFormat:@"Inbox"];
      [indexarray removeObject:indexPath];
      if (selectedDeleteCardCount != 0){
      cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
      if (deleBtn) {[deleBtn setEnabled:true];}
      }else {
      if (deleBtn) {[deleBtn setEnabled:false];}
      }
      [self.navigationController.navigationBar.topItem setTitle:cardsCount];
      }


      any clue will be helpful. thanks.










      share|improve this question














      I have an issue where some cells are deselected when i try to reload the rows/table with refresh controller and scroll the tableview immediately. i checked it with the deselection in diSelectrow and in didDeselectRow.In both methods the debug counter isn't encountered.
      i have the following code in cellforRow



      if (isEditClicked == true || [indexarray containsObject:indexPath]) {
      [cardinboxCardCell setSelectionStyle:UITableViewCellSelectionStyleDefault];
      }


      following in didselectrow



      if (!self.tableView.isEditing) {
      Card *card;
      [tableView deselectRowAtIndexPath:indexPath animated:YES];
      card = [_fetchedResultsController objectAtIndexPath:indexPath];
      [self performSegueWithIdentifier:@"pushToMyCardDetails" sender:card];
      }else {
      selectedDeleteCardCount += 1;
      if (deleBtn) {
      [deleBtn setEnabled:true];
      }
      [indexarray addObject:indexPath];
      NSString* cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
      [self.navigationController.navigationBar.topItem setTitle:cardsCount];
      }


      following in didDeselctrow



      if (self.tableView.isEditing) {
      selectedDeleteCardCount -= 1;
      NSString* cardsCount = [NSString stringWithFormat:@"Inbox"];
      [indexarray removeObject:indexPath];
      if (selectedDeleteCardCount != 0){
      cardsCount = [NSString stringWithFormat:@"(%d)",selectedDeleteCardCount];
      if (deleBtn) {[deleBtn setEnabled:true];}
      }else {
      if (deleBtn) {[deleBtn setEnabled:false];}
      }
      [self.navigationController.navigationBar.topItem setTitle:cardsCount];
      }


      any clue will be helpful. thanks.







      ios objective-c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 6:22









      kncixkncix

      1




      1
























          4 Answers
          4






          active

          oldest

          votes


















          0














          I have doubt over indexpath object, might be this object has changed.. so thats why this condition is failing



          if (isEditClicked == true || [indexarray containsObject:indexPath]) {


          My advice to you is check with section and row index not for object itself like this



          //find indexPath (row and section) in indexarray using for loop to retrive whether 
          //is this cell is selected or not

          bool isCellSelected = [self currentIndexPathSelected:indexPath];
          if (isEditClicked == true || isCellSelected) {
          }





          share|improve this answer
























          • Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

            – kncix
            Nov 22 '18 at 5:09





















          0














          You just have to maintain States for all cell. You can use Bool isSelected. Whenever you select any cell mark it true at index of state. same as deselect mark it false. :-)






          share|improve this answer
























          • am already maintaining the selected cell indexes in indexarray.

            – kncix
            Nov 21 '18 at 7:01



















          0














          Don't try and maintain the selected state in the UI. Store it independently so it can be restored when you reload the tableview.



          I would in general advise against storing the selected indexes or indexPaths as the relationship between these and your model can change if items are inserted/deleted/reordered. (may not apply in your case though, but still better to avoid this potential issue)



          Instead store the selected model objects, then you will always know exactly what is selected and nothing can mess with this. Just make the mutable array 'selected' and add remove them as needed.



          didSelectRowAtIndexPath is where you add/remove the model object from the selected array.
          cellForRowAtIndexPath is where you set the cell selected state dependant on the selected array.






          share|improve this answer































            -1














            IN order to achieve what you are looking for, your set up should look something like this:



            enter image description here



            Now focus on this part:



            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
            NSDictionary *object = _objects[indexPath.row];
            cell.textLabel.text = object[@"text"];
            cell.imageView.image = [UIImage imageNamed:object[@"image"]];
            if (_selectedIndexPaths[[self getKey:indexPath]] != nil) cell.selected = [_selectedIndexPaths[indexPath] boolValue];
            return cell;
            }

            - (NSString *) getKey : (NSIndexPath *) path {
            return [NSString stringWithFormat:@"%ld-%ld", (long)path.section, (long)path.row];
            }

            - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
            [_selectedIndexPaths setValue:@(TRUE) forKey:[self getKey:indexPath]];
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            [cell setSelected:true];

            //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }


            As you can see we set a value in _selectedIndexPaths while cell get selected, and when in cell is constructed that check allow us to keep selection alive. You can use same pattern with checkMark accessory as well.






            share|improve this answer























              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',
              autoActivateHeartbeat: false,
              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%2f53406309%2ftableviewcontroller-selective-cells-gets-deselected-on-table-relaod%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I have doubt over indexpath object, might be this object has changed.. so thats why this condition is failing



              if (isEditClicked == true || [indexarray containsObject:indexPath]) {


              My advice to you is check with section and row index not for object itself like this



              //find indexPath (row and section) in indexarray using for loop to retrive whether 
              //is this cell is selected or not

              bool isCellSelected = [self currentIndexPathSelected:indexPath];
              if (isEditClicked == true || isCellSelected) {
              }





              share|improve this answer
























              • Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

                – kncix
                Nov 22 '18 at 5:09


















              0














              I have doubt over indexpath object, might be this object has changed.. so thats why this condition is failing



              if (isEditClicked == true || [indexarray containsObject:indexPath]) {


              My advice to you is check with section and row index not for object itself like this



              //find indexPath (row and section) in indexarray using for loop to retrive whether 
              //is this cell is selected or not

              bool isCellSelected = [self currentIndexPathSelected:indexPath];
              if (isEditClicked == true || isCellSelected) {
              }





              share|improve this answer
























              • Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

                – kncix
                Nov 22 '18 at 5:09
















              0












              0








              0







              I have doubt over indexpath object, might be this object has changed.. so thats why this condition is failing



              if (isEditClicked == true || [indexarray containsObject:indexPath]) {


              My advice to you is check with section and row index not for object itself like this



              //find indexPath (row and section) in indexarray using for loop to retrive whether 
              //is this cell is selected or not

              bool isCellSelected = [self currentIndexPathSelected:indexPath];
              if (isEditClicked == true || isCellSelected) {
              }





              share|improve this answer













              I have doubt over indexpath object, might be this object has changed.. so thats why this condition is failing



              if (isEditClicked == true || [indexarray containsObject:indexPath]) {


              My advice to you is check with section and row index not for object itself like this



              //find indexPath (row and section) in indexarray using for loop to retrive whether 
              //is this cell is selected or not

              bool isCellSelected = [self currentIndexPathSelected:indexPath];
              if (isEditClicked == true || isCellSelected) {
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 '18 at 6:33









              SachinVsSachinSachinVsSachin

              4,97612735




              4,97612735













              • Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

                – kncix
                Nov 22 '18 at 5:09





















              • Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

                – kncix
                Nov 22 '18 at 5:09



















              Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

              – kncix
              Nov 22 '18 at 5:09







              Hi, i have checked and got the logs of index paths of selected rows, even when i have three index in array, sometime none are selected and sometimes all are selected and sometimes only partially two rows are selected and one row isn't selcted.

              – kncix
              Nov 22 '18 at 5:09















              0














              You just have to maintain States for all cell. You can use Bool isSelected. Whenever you select any cell mark it true at index of state. same as deselect mark it false. :-)






              share|improve this answer
























              • am already maintaining the selected cell indexes in indexarray.

                – kncix
                Nov 21 '18 at 7:01
















              0














              You just have to maintain States for all cell. You can use Bool isSelected. Whenever you select any cell mark it true at index of state. same as deselect mark it false. :-)






              share|improve this answer
























              • am already maintaining the selected cell indexes in indexarray.

                – kncix
                Nov 21 '18 at 7:01














              0












              0








              0







              You just have to maintain States for all cell. You can use Bool isSelected. Whenever you select any cell mark it true at index of state. same as deselect mark it false. :-)






              share|improve this answer













              You just have to maintain States for all cell. You can use Bool isSelected. Whenever you select any cell mark it true at index of state. same as deselect mark it false. :-)







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 '18 at 6:56









              Niraj SolankiNiraj Solanki

              597




              597













              • am already maintaining the selected cell indexes in indexarray.

                – kncix
                Nov 21 '18 at 7:01



















              • am already maintaining the selected cell indexes in indexarray.

                – kncix
                Nov 21 '18 at 7:01

















              am already maintaining the selected cell indexes in indexarray.

              – kncix
              Nov 21 '18 at 7:01





              am already maintaining the selected cell indexes in indexarray.

              – kncix
              Nov 21 '18 at 7:01











              0














              Don't try and maintain the selected state in the UI. Store it independently so it can be restored when you reload the tableview.



              I would in general advise against storing the selected indexes or indexPaths as the relationship between these and your model can change if items are inserted/deleted/reordered. (may not apply in your case though, but still better to avoid this potential issue)



              Instead store the selected model objects, then you will always know exactly what is selected and nothing can mess with this. Just make the mutable array 'selected' and add remove them as needed.



              didSelectRowAtIndexPath is where you add/remove the model object from the selected array.
              cellForRowAtIndexPath is where you set the cell selected state dependant on the selected array.






              share|improve this answer




























                0














                Don't try and maintain the selected state in the UI. Store it independently so it can be restored when you reload the tableview.



                I would in general advise against storing the selected indexes or indexPaths as the relationship between these and your model can change if items are inserted/deleted/reordered. (may not apply in your case though, but still better to avoid this potential issue)



                Instead store the selected model objects, then you will always know exactly what is selected and nothing can mess with this. Just make the mutable array 'selected' and add remove them as needed.



                didSelectRowAtIndexPath is where you add/remove the model object from the selected array.
                cellForRowAtIndexPath is where you set the cell selected state dependant on the selected array.






                share|improve this answer


























                  0












                  0








                  0







                  Don't try and maintain the selected state in the UI. Store it independently so it can be restored when you reload the tableview.



                  I would in general advise against storing the selected indexes or indexPaths as the relationship between these and your model can change if items are inserted/deleted/reordered. (may not apply in your case though, but still better to avoid this potential issue)



                  Instead store the selected model objects, then you will always know exactly what is selected and nothing can mess with this. Just make the mutable array 'selected' and add remove them as needed.



                  didSelectRowAtIndexPath is where you add/remove the model object from the selected array.
                  cellForRowAtIndexPath is where you set the cell selected state dependant on the selected array.






                  share|improve this answer













                  Don't try and maintain the selected state in the UI. Store it independently so it can be restored when you reload the tableview.



                  I would in general advise against storing the selected indexes or indexPaths as the relationship between these and your model can change if items are inserted/deleted/reordered. (may not apply in your case though, but still better to avoid this potential issue)



                  Instead store the selected model objects, then you will always know exactly what is selected and nothing can mess with this. Just make the mutable array 'selected' and add remove them as needed.



                  didSelectRowAtIndexPath is where you add/remove the model object from the selected array.
                  cellForRowAtIndexPath is where you set the cell selected state dependant on the selected array.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 1:40









                  trappertrapper

                  7,39452463




                  7,39452463























                      -1














                      IN order to achieve what you are looking for, your set up should look something like this:



                      enter image description here



                      Now focus on this part:



                      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
                      NSDictionary *object = _objects[indexPath.row];
                      cell.textLabel.text = object[@"text"];
                      cell.imageView.image = [UIImage imageNamed:object[@"image"]];
                      if (_selectedIndexPaths[[self getKey:indexPath]] != nil) cell.selected = [_selectedIndexPaths[indexPath] boolValue];
                      return cell;
                      }

                      - (NSString *) getKey : (NSIndexPath *) path {
                      return [NSString stringWithFormat:@"%ld-%ld", (long)path.section, (long)path.row];
                      }

                      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
                      [_selectedIndexPaths setValue:@(TRUE) forKey:[self getKey:indexPath]];
                      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                      [cell setSelected:true];

                      //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                      }


                      As you can see we set a value in _selectedIndexPaths while cell get selected, and when in cell is constructed that check allow us to keep selection alive. You can use same pattern with checkMark accessory as well.






                      share|improve this answer




























                        -1














                        IN order to achieve what you are looking for, your set up should look something like this:



                        enter image description here



                        Now focus on this part:



                        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
                        NSDictionary *object = _objects[indexPath.row];
                        cell.textLabel.text = object[@"text"];
                        cell.imageView.image = [UIImage imageNamed:object[@"image"]];
                        if (_selectedIndexPaths[[self getKey:indexPath]] != nil) cell.selected = [_selectedIndexPaths[indexPath] boolValue];
                        return cell;
                        }

                        - (NSString *) getKey : (NSIndexPath *) path {
                        return [NSString stringWithFormat:@"%ld-%ld", (long)path.section, (long)path.row];
                        }

                        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
                        [_selectedIndexPaths setValue:@(TRUE) forKey:[self getKey:indexPath]];
                        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                        [cell setSelected:true];

                        //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                        }


                        As you can see we set a value in _selectedIndexPaths while cell get selected, and when in cell is constructed that check allow us to keep selection alive. You can use same pattern with checkMark accessory as well.






                        share|improve this answer


























                          -1












                          -1








                          -1







                          IN order to achieve what you are looking for, your set up should look something like this:



                          enter image description here



                          Now focus on this part:



                          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
                          NSDictionary *object = _objects[indexPath.row];
                          cell.textLabel.text = object[@"text"];
                          cell.imageView.image = [UIImage imageNamed:object[@"image"]];
                          if (_selectedIndexPaths[[self getKey:indexPath]] != nil) cell.selected = [_selectedIndexPaths[indexPath] boolValue];
                          return cell;
                          }

                          - (NSString *) getKey : (NSIndexPath *) path {
                          return [NSString stringWithFormat:@"%ld-%ld", (long)path.section, (long)path.row];
                          }

                          - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
                          [_selectedIndexPaths setValue:@(TRUE) forKey:[self getKey:indexPath]];
                          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                          [cell setSelected:true];

                          //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                          }


                          As you can see we set a value in _selectedIndexPaths while cell get selected, and when in cell is constructed that check allow us to keep selection alive. You can use same pattern with checkMark accessory as well.






                          share|improve this answer













                          IN order to achieve what you are looking for, your set up should look something like this:



                          enter image description here



                          Now focus on this part:



                          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
                          NSDictionary *object = _objects[indexPath.row];
                          cell.textLabel.text = object[@"text"];
                          cell.imageView.image = [UIImage imageNamed:object[@"image"]];
                          if (_selectedIndexPaths[[self getKey:indexPath]] != nil) cell.selected = [_selectedIndexPaths[indexPath] boolValue];
                          return cell;
                          }

                          - (NSString *) getKey : (NSIndexPath *) path {
                          return [NSString stringWithFormat:@"%ld-%ld", (long)path.section, (long)path.row];
                          }

                          - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
                          [_selectedIndexPaths setValue:@(TRUE) forKey:[self getKey:indexPath]];
                          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                          [cell setSelected:true];

                          //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                          }


                          As you can see we set a value in _selectedIndexPaths while cell get selected, and when in cell is constructed that check allow us to keep selection alive. You can use same pattern with checkMark accessory as well.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 7:41









                          rptwsthirptwsthi

                          8,56885392




                          8,56885392






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406309%2ftableviewcontroller-selective-cells-gets-deselected-on-table-relaod%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







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              Npm cannot find a required file even through it is in the searched directory