iOS Segue opening two view controllers (assuming two table cells clicked)

348 views Asked by At

When I click a table cell in my application it opens two view controllers. Which it seems to assume that I have clicked two cells, since it opens the cell before and the one I have clicked.

The first 3 cells are pretty much static, every other cell underneath the "ingredients" heading are dynamic. Hence why indexPath - 3

I created the segue between the table view and the view controller in the interface builder.

Table View:

enter image description here

First view controller/segue:

enter image description here

Second view controller segue:

enter image description here

Code for the segue:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIngredient =  self.selectedProduct.ingredientsArray[indexPath.row - 3];
    [self performSegueWithIdentifier:@"ingViewSegue" sender:self];
    NSLog(@"selected ingredient %@", self.selectedIngredient);
 }

  #pragma mark segue

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     IngredientWebViewController *webView = segue.destinationViewController;
     webView.selectedIngredient = self.selectedIngredient;
 }

Number of rows in table:

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
       return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   return [_selectedProduct.ingredientsArray count] + 3;
}

Code to populate table:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        TitleCell* titleCell = (TitleCell*)[self.tableView dequeueReusableCellWithIdentifier:@"TitleCell" forIndexPath:indexPath];
        titleCell.nameLabel.text = self.selectedProduct.productName;
        titleCell.brandLabel.text = self.selectedProduct.productBrand;
        return titleCell;
    }
    else if(indexPath.row == 1)
    {
        return [self.tableView dequeueReusableCellWithIdentifier:@"RatingCell" forIndexPath:indexPath];

    }
    else if(indexPath.row == 2)
    {
        return [self.tableView dequeueReusableCellWithIdentifier:@"IngredientHeaderCell" forIndexPath:indexPath];
    }
    else
    {
        IngredientsCell* ingCell = (IngredientsCell*)[self.tableView dequeueReusableCellWithIdentifier:@"IngredientCell" forIndexPath:indexPath];

        ingCell.ingredientNameLabel.text =     self.selectedProduct.ingredientsArray[indexPath.row - 3];

        if([self.selectedProduct.ratingsArray[indexPath.row -3]  isEqual: @"4"])
        {
            ingCell.ingredientsImage.image = [UIImage imageNamed:@"icon_safe.png"];
        }
        else if([self.selectedProduct.ratingsArray[indexPath.row -3]  isEqual: @"3"])
        {
            ingCell.ingredientsImage.image = [UIImage imageNamed:@"icon_low.png"];
        }
        else if([self.selectedProduct.ratingsArray[indexPath.row -3]  isEqual: @"2"])
        {
            ingCell.ingredientsImage.image = [UIImage imageNamed:@"icon_med.png"];
        }
        else if([self.selectedProduct.ratingsArray[indexPath.row -3]  isEqual: @"1"])
        {
            ingCell.ingredientsImage.image = [UIImage imageNamed:@"icon_high.png"];
        }
        else
        {
            ingCell.ingredientsImage.image = [UIImage imageNamed:@"icon_safe.png"];
        }

        return ingCell;
    }
}

Code to alter cell height:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
       UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"TitleCell"];
       CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
       return size.height + 1;
   }
   else if(indexPath.row == 1)
   {
       UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"RatingCell"];
       CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
       return 118;
   }
   else if(indexPath.row == 2)
   {
       UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"IngredientHeaderCell"];
       CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
       return size.height + 1;
   }
   else
   {
       UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"IngredientCell"];
       CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
       return 60;
   }
 }
1

There are 1 answers

2
Bannings On BEST ANSWER

A segue will be performed automatically while a cell is tapped. Try the following code:

// - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// {
//     self.selectedIngredient =  self.selectedProduct.ingredientsArray[indexPath.row - 3];
//     [self performSegueWithIdentifier:@"ingViewSegue" sender:self];
//     NSLog(@"selected ingredient %@", self.selectedIngredient);
//  }

  #pragma mark segue

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 

     IngredientWebViewController *webView = segue.destinationViewController;
     webView.selectedIngredient = self.selectedProduct.ingredientsArray[indexPath.row - 3];
 }