当我单击应用程序中的表格单元格时,它会打开两个视图控制器。它似乎假设我已经点击了两个单元格,因为它之前打开了单元格,并且我点击了单元格。
前三个细胞几乎是静止的,“成分”标题下的每个其他细胞都是动态的。因此为什么indexPath - 3
我在界面构建器中的表视图和视图控制器之间创建了segue。
表视图:
第一个视图controller / segue:
第二个视图控制器segue:
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;
}
表格中的行数:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_selectedProduct.ingredientsArray count] + 3;
}
填充表格的代码:
- (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;
}
}
答案 0 :(得分:1)
在点击一个单元格时将自动执行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 {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
IngredientWebViewController *webView = segue.destinationViewController;
webView.selectedIngredient = self.selectedProduct.ingredientsArray[indexPath.row - 3];
}