Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually in <code>tableView:numberOfRowsInSection:</code> you are returning the count of the entire <code>exercises</code> array. So with your sample data you would have two rows per section. Try making an array of exercises for every section and then code something like the following:</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. if (section == 0) { return [self.chestExercises count]; } else if (section == 1) { return [self.bicepsExercises count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ExercisePrototypeCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... MFTExercises *exercise; if (indexPath.section == 0) { exercise = [self.chestExercises objectAtIndex:indexPath.row]; } else if (indexPath.section == 1) { exercise = [self.bicepsExercises objectAtIndex:indexPath.row]; } cell.textLabel.text = exercise.exerciseName; return cell; } </code></pre> <p>In this case the <code>chestExercises</code> array would only contain the "Bench Press"-exercise and the <code>bicepsExercises</code> would only contain the "Barbell Curl"-exercise. So you would get one row per section.</p> <p>For achieving that the sections have titles you would need to implement the method</p> <pre><code>- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section { return [self.exerciseCategories objectAtIndex:section]; } </code></pre> <p>which gives the sections the title according to the names stored in the array.</p> <p>A more sophisticated way to build your datasource would be to create a <code>NSDictionary</code> with the section names as the keys (bodyparts) and the values being arrays containing the exercises for the bodypart. For instance if your categories are merely strings you could build such a dictionary with your sample data (for the purpose of demonstration I added another exercise):</p> <pre><code>FNTExerciseCategories *category1 = [[FNTExerciseCategories alloc]init]; category1.exerciseCategoryName = @"Chest"; [self.exerciseCategories addObject:category1]; FNTExerciseCategories *category2 = [[FNTExerciseCategories alloc]init]; category2.exerciseCategoryName = @"Biceps"; [self.exerciseCategories addObject:category2]; FNTExercises *exercise1 = [[FNTExercises alloc]init]; exercise1.exerciseName = @"Bench Press"; FNTExercises *exercise2 = [[FNTExercises alloc]init]; exercise2.exerciseName = @"Barbell Curl"; FNTExercises *exercise3 = [[FNTExercises alloc]init]; exercise3.exerciseName = @"Another Exercise"; // the instance variable self.exercises is a NSMutableDictionary now of course self.exercises = [[NSMutableDictionary alloc] init]; exercises[category1.exerciseCategoryName] = @[exercise1]; exercises[category2.exerciseCategoryName] = @[exercise2, exercise3]; </code></pre> <p>The advantage here is that you now have one dictionary containing all arrays that contains all your data. So as you're adding more data you don't have to change your implementation of the tableView datasource. BTW I am using Modern Objective-C syntax for the dictionary and arrays. </p> <p>Having created a dictionary like that you could then simply implement your table view data source like so:</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. // This gives the name of the category at the current section. // It is then used as a key for the dictionary. NSString *currentCategory = [[self.exerciseCategories objectAtIndex:section] exerciseCategoryName]; return [self.exercises[currentCategory] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ExercisePrototypeCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... NSString *currentCategory = [[self.exerciseCategories objectAtIndex:indexPath.section] exerciseCategoryName]; MFTExercises *exercise = [self.exercises[currentCategory] objectAtIndex:indexPath.row]; cell.textLabel.text = exercise.exerciseName; return cell; } </code></pre> <p>Using a <code>NSDictionary</code> may or may not benefit your app but you don't have to create an array as instance variable for every body part you have. It may also be more easy to save a single dictionary to disk for persistence.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload