Conditional Segue navigation from UITableViewCell based on response to UIAlertView

后端 未结 3 1907
说谎
说谎 2021-02-03 14:06

My problem seems like a generic problem, yet can\'t seem to find an answer for it.

I have a situation where when the user taps on a custom UITableViewCell, I would like

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-03 14:47

    OK I came up with a solution in keeping with the storyboard that I like.

    Example:

    My tableview has 2 sections, grouped, and cells are dynamic prototype. Section 0 contains one row/UITableViewCell & I don't want it to segue. Section 1 contains multiple cells that I want to trigger the segue & drill down into the detail.

    In Storyboard:

    • I removed the segue linking the tableviewcell to the destination view controller.
    • I made a 'generic' segue linking the source view controller directly to the destination view controller.
    • In the attributes on the segue, I set the identifier ('EditTimePeriod') and set the type to Push (I presume Modal would work just the same).

    In the source view controller:

    • In the prepareForSegue method I handled both the common 'AddTimePeriod' segue I control-dragged from my UIBarButtonItem (Add), along with the 'generic'(vc-->vc) 'EditTimePeriod' segue.

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      
          // handle the click of the 'Add' bar button item    
          if([segue.identifier isEqualToString:@"AddTimePeriod"]) {
              TimePeriodViewController* tpvc = (TimePeriodViewController*)segue.destinationViewController;
              tpvc.delegate = self;
              // database & entity stuff for adding the new one to the mOC, etc
          }
      
          // handle the click of one of the 'editable' cells - 
          if([segue.identifier isEqualToString:@"EditTimePeriod"]) {
              TimePeriodViewController* tpvc = (TimePeriodViewController*)segue.destinationViewController;
              tpvc.delegate = self;
              TimePeriod * newTP = [self.timePeriodArray objectAtIndex:self.tableView.indexPathForSelectedRow.row];
              tpvc.timePeriod = newTP;
          }
      }   
      
    • Then I implemented the tableView:didSelectRowAtIndexPath method, and put my condition in here. If the selected row was outside of section zero I called the EditTimePeriod segue manually, defining the sender as the selected tableviewcell:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      
          if(self.tableView.indexPathForSelectedRow.section!=0){
              [self performSegueWithIdentifier:@"EditTimePeriod" sender:[tableView cellForRowAtIndexPath:indexPath]];
          }
      
          [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
      
          return;    
      }
      

    would be nice to code the cell in section 0 so that it is not selectable in the first place! Hope this helps though.

    ** and then 5 minutes later I took another look and realized I could just move the data from section 0 into the section header, which is more intuitive and wasn't being used anyway. leaving the design open for a standard segue from each tableviewcell without needing any condition/check. Was a good exercise anyway though :)

提交回复
热议问题