【发布时间】:2015-03-29 00:02:27
【问题描述】:
简单的食谱应用程序
在我的 recipeBook 应用程序中,我有两个 UITableViewController。第一个 UITableViewController 包含一个带有配方名称列表的 UITableView。如果您选择一个单元格,您将转到第二个 UITableViewController。第二个 UITableViewController 包含一个带有成分列表的 UITableView。
在我的应用程序中,我使用了一个 RecipeObject 类,它包含两个属性(名称(类型:NSString)和成分(类型:NSArray)。recipeObject 对象都在 RecipeData 类中声明,如下所示:
RecipeObject *recipe1 = [[RecipeObject alloc]init];
recipe1.name = @"Fresh Coconut Cake";
recipe1.ingredients = [NSArray arrayWithObjects:@"Coconut cups", @"Milk", @"Baking powder", @"Butter", @"Sugar", @"Eggs", nil];
在第一个 UITableViewController 中,我设法打印出每个单元格的配方名称。见以下代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"recipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
TostiObject *recipes = [self.recipes objectAtIndex:indexPath.row];
cell.textLabel.text = recipes.name;
return cell;
我在第一个 UITableViewController 中声明了一个名为 self.recipes 的 NSArray 属性来连接 RecipeData。我还在第二个 UITableViewController 中做了以下操作。
self.recipes = [RecipeData allRecipes];
问题出现在第二个UITableViewController:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"ingredientCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
cell.textLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];
return cell;
因为 cell.textLabel.text 只接受一个 NSString 值,而 recipe.ingredients 是一个 NSArray。我想将成分数据(一个 NSArray)连接到第二个 UITableViewController 中的 cell.textLabel.text。
也许有人可以帮我解决这个问题。非常感谢所有帮助!
- 其他信息-
prepareForSegue 方法(第一个 UITableViewController):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ingredients"]) {
SecondTableViewController *IngredientsTVC = (SecondTableViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
IngredientsTVC.recipeContainer = recipe.ingredients;
【问题讨论】:
-
标题问题的答案是,你不能;您只能将字符串传递给 text 属性,而不是数组。
标签: ios objective-c uitableview nsarray cell