Tuesday, 14 June 2016

Table View Controller - Use Case 2 - in iOS Objective C

Use Case - 2
        |
     Main
        |
UI View Controller
        |
UI View
        |
UITableView
        |
UITable View Cell
       |
UI Components
==================
1) Create Single View Application
2) Drag & Drop Table View on UiView Controller

3) Drag & Drop Table View Cell  on Table View

4) InViewController.h file write below code

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *tableObj;
    NSArray *idevices,*androidDevices;
    NSString *mess;
}

@end





5) In ViewController.m write below code
- (void)viewDidLoad {
    
    tableObj.dataSource=self;
    tableObj.delegate=self;
    
  
    idevices=[NSArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",@"iPadmini", nil];
    androidDevices=[NSArray arrayWithObjects:@"HTC",@"samsung",@"Lenevo",@"Nexus", nil];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //return 5;
    if (section==0) {
        return idevices.count;
    }
    else
    {
        return androidDevices.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    // Configure the cell...
    // cell.textLabel.text=@"Hello iOS SDK";
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.font=[UIFont fontWithName:@"Verdana" size:26];
    cell.textLabel.textColor=[UIColor blueColor];
  //  cell.backgroundColor=[UIColor yellowColor];
//    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
    
    if (indexPath.section==0) {
        cell.textLabel.text=[idevices objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text=[androidDevices objectAtIndex:indexPath.row];
    }
    return cell;
}
// delegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        mess=[NSString stringWithFormat:@"you liked..%@",[idevices objectAtIndex:indexPath.row]];
    }
    else
    {
           mess=[NSString stringWithFormat:@"you liked..%@",[androidDevices objectAtIndex:indexPath.row]];
    }
     UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"Hello User"
                                      message:mess
                                      preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* ok = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                                 // navigation for another controller
                                 
                             }];
        UIAlertAction* cancel = [UIAlertAction
                                 actionWithTitle:@"Cancel"
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     [alert dismissViewControllerAnimated:YES completion:nil];
                                     
                                 }];
        
        [alert addAction:ok];
        [alert addAction:cancel];
        
        [self presentViewController:alert animated:YES completion:nil];

}

6) Right Click on Table View --> Map referencing outlet to tableObj

7) Click on Table Cell -->Identifier-->MyCell

8) Build & Run Application

No comments:

Post a Comment