ios - How to make NSArray an acceptable value for cell.textLabel.text? -
simple recipebook application
in recipebook application have 2 uitableviewcontrollers. first uitableviewcontroller contains uitableview list of recipe names. if select cell segue second uitableviewcontroller. second uitableviewcontroller contains uitableview list of ingredients.
in application use recipeobject class contains 2 properties (name (type: nsstring) , ingredients (type: nsarray). recipeobject objects declared in recipedata class, this:
recipeobject *recipe1 = [[recipeobject alloc]init]; recipe1.name = @"fresh coconut cake"; recipe1.ingredients = [nsarray arraywithobjects:@"coconut cups", @"milk", @"baking powder", @"butter", @"sugar", @"eggs", nil];
in first uitableviewcontroller managed print out recipe names each cell. see following code:
-(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;
i declared nsarray property in first uitableviewcontroller called self.recipes connect recipedata. did following in second uitableviewcontroller.
self.recipes = [recipedata allrecipes];
the problem occurs in second 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;
because cell.textlabel.text accepts nsstring value , recipe.ingredients nsarray. want connect ingredients data (an nsarray) cell.textlabel.text in second uitableviewcontroller.
maybe can me solve problem. highly appreciated!
- other information -
the prepareforsegue method (first 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;
you should passing 1 recipe object second table view controller (so don't understand why you're using recipeobject *recipe = [self.recipes objectatindex:indexpath.row]
in cellforrowatindexpath
). can use recipe.ingredients array use populate table. should still able use line in cellfrorowatindexpath,
cell.textlabel.text = [recipe.ingredients objectatindex:indexpath.row];
you should passing recipe.ingredients.count
numberofrowsinsection
.
after edit:
several things not clear you're doing. seems want secondtableviewcontroller display list of ingredients of single selected recipe first controller. so, first of all, there's no need have line self.recipes = [recipedata allrecipes]
in secondtableviewcontroller @ all; controller needs know 1 recipe passed in via prepareforsegue. assume recipecontainer array property of secondtableviewcontroller array of ingredients; need populate table (btw, there's no need pass in prepareforsegue, since can recipe object passed). so, way should is, first, drop last line show in prepareforsegue; don't need there.
in secondtableviewcontroller .h
@property (strong,nonatomic) recipeobject *recipe;
in secondtableviewcontroller .m
@interface secondtablecontroller () @property (strong,nonatomic) nsarray *ingredients; @end @implementation secondtablecontroller - (void)viewdidload { [super viewdidload]; self.ingredients = self.recipe.ingredients; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return self.ingredients.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; cell.textlabel.text = self.ingredients[indexpath.row]; return cell; }
if want, set title of secondviewcontroller in viewdidload using self.title = recipe.name
Comments
Post a Comment