iOS Segue 打开两个视图控制器(假设单击了两个 table 单元格)

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

当我单击应用程序中的 table 单元格时,它会打开两个视图控制器。它似乎假设我点击了两个单元格,因为它打开了之前的单元格和我点击过的单元格。

前 3 个单元格几乎是静态的,"ingredients" 标题下的所有其他单元格都是动态的。因此为什么 indexPath - 3

我在界面生成器中创建了 table 视图和视图控制器之间的连接。

Table 查看:

第一个视图controller/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;
 }

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;
}

要填充的代码 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;
    }
}

改变单元格高度的代码:

-(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;
   }
 }

点击单元格时将自动执行转场。试试下面的代码:

// - (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];
 }