Saturday, 5 April 2014



Write a program to create result of student using if-else & switch.

studentsViewController.h

 #import <UIKit/UIKit.h>

@interface studentsViewController : UITableViewController

@end


studentsViewController.m

#import "studentsViewController.h"

@interface studentsViewController ()

@end

@implementation studentsViewController{
    NSMutableArray *students;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    students = [[NSMutableArray alloc]initWithObjects:
                                                   @{@"name": @"Student 1", @"percentage": @"40"},
                                                   @{@"name": @"Student 2", @"percentage": @"50"},
                                                   @{@"name": @"Student 3", @"percentage": @"60"}, nil];
   
    self.tableView.rowHeight = 60;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [students count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
   
    NSDictionary *dict = [students objectAtIndex:indexPath.row];
   
    int per = [[dict objectForKey:@"percentage"] integerValue];
   
    NSString *grade;
    if(per >= 75){
        grade = @"A";
    } else if(per >= 60 && per<75){
        grade = @"B";
    } else {
        grade = @"C";
    }
   
   
    UILabel *nameLbl = [[UILabel alloc]init];
    nameLbl.textColor = [UIColor blackColor];
    nameLbl.text = [dict objectForKey:@"name"];
    nameLbl.font = [UIFont boldSystemFontOfSize:20];
    nameLbl.frame = CGRectMake(15, 7, 220, 25);
    [cell addSubview:nameLbl];
   
    UILabel *percentageLbl = [[UILabel alloc]init];
    percentageLbl.textColor = [UIColor grayColor];
    percentageLbl.text = [dict objectForKey:@"percentage"];
    percentageLbl.font = [UIFont boldSystemFontOfSize:16];
    percentageLbl.frame = CGRectMake(15, 33, 220, 21);
    [cell addSubview:percentageLbl];
   
    UILabel *gradeLbl = [[UILabel alloc]init];
    gradeLbl.textColor = [UIColor blackColor];
    gradeLbl.text = grade;
    gradeLbl.font = [UIFont boldSystemFontOfSize:18];
    gradeLbl.frame = CGRectMake(229, 21, 30, 21);
    [cell addSubview:gradeLbl];
   
    return cell;
}

@end


No comments:

Post a Comment