Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - Simple Table Sections with Array Data
    text
    copied!<p>I'm just starting with iOS/Xcode and have been Googling/Youtubing for an hour and can't find a matching tutorial. All I'm trying to do right now is display a table with a list of exercises (rows) that are grouped by bodypart (sections). The bodypart sections will never change, but the user will be able to add a custom exercise to a bodypart.</p> <p>Now, I'm assuming that I need an array for the sections and also an array for exercises...creating those is simple enough. I'm running into a problem assigning exercises to specific sections. Here's an example of the faulty code that when rendered, displays both exercises under both sections...also there aren't any section names being generated in the table so I'm not sure where that comes into play either.</p> <p>Here's a screenshot of the result (as a side note, not sure why my nav controller isn't rendering): <a href="http://i.imgur.com/icoJgEq.jpg" rel="nofollow">http://i.imgur.com/icoJgEq.jpg</a></p> <p>Create the individual items:</p> <pre><code>@property NSString *exerciseName; @property NSString *exerciseCategoryName; </code></pre> <p>Create/Allocate the arrays:</p> <pre><code>@property NSMutableArray *exerciseCategories; @property NSMutableArray *exercises; self.exerciseCategories = [[NSMutableArray alloc]init]; self.exercises = [[NSMutableArray alloc]init]; </code></pre> <p>Fill the arrays with some default data:</p> <pre><code>- (void)loadInitialData { 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"; [self.exercises addObject:exercise1]; FNTExercises *exercise2 = [[FNTExercises alloc]init]; exercise2.exerciseName = @"Barbell Curl"; [self.exercises addObject:exercise2]; } </code></pre> <p>Load the data:</p> <pre><code>[self loadInitialData]; #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [self.exerciseCategories count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [self.exercises count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"ExercisePrototypeCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... MFTExercises *exercise = [self.exercises objectAtIndex:indexPath.row]; cell.textLabel.text = exercise.exerciseName; return cell; } </code></pre> <p>Thank you very much to anybody that can chime in!</p>
 

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