Tuesday, May 22, 2012

iPhone Code (initializing a TableViewController)

carbData = [[NSArray alloc] 
initWithObjects:@"What Are Carbs?",@"Why Do I Need Carbs?",@"Simple Vs. Complex",
@"Fun Five Facts",nil]; 
 
proteinData =  [[NSArray alloc] 
initWithObjects:@"What is Protein?",@"Why Do I Need Protein?",
@"Complete Vs. Incomplete",@"Fun Five Facts",nil]; 
 
fatData = [[NSArray alloc] 
initWithObjects:@"What Are Fats?",@"Why Do I Need Fats?",
@"Saturated Vs. Unsaturated",@"Fun Five Facts",nil];  
 
[self setTitle:@"Nutrition Table"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section
{
    return [self.carbData count];
    return [self.proteinData count];
    return [self.fatData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
 SimpleTableIdentifier];

    if (cell == nil) 
 { cell = [[[UITableViewCell alloc] 
initWithFrame:CGRectZero 
reuseIdentifier:SimpleTableIdentifier] autorelease];
 }
    
    if(indexPath.section == 0)
        cell.text = [carbData objectAtIndex:indexPath.row];
    if(indexPath.section == 1)
        cell.text = [proteinData objectAtIndex:indexPath.row];
    if (indexPath.section == 2)
        cell.text = [fatData objectAtIndex:indexPath.row];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section
 {
    if(section == 0)
        return @"Carbohydrates";
    if (section ==1)
        return @"Protein";
    else if (section ==2)
        return @"Fats";
}

No comments:

Post a Comment