Wednesday, 15 June 2016

TableView Controller - Use Case3 - in iOS - Objective C

Use Case3
       |
Main
       |
UI Table View Controller
      |
UI Table View
      |
UI Table View Cell (wrong X)
      |
Custom Cell
      |
Custom Components
=============================
1) Implement Use Case1 as it is
2) Right click on table controller have a new file, iOS source cocoa touch class
3) Click next button, class name "MyCell", SubClass of UITableViewCell
4) Use checkmark also create Xib File, click on next
5) go to MyCell.Xib design following
     1) Product Name  -- Label
     2) Product Price   -- Label'
     3) Description      -- Label
     4) Image               -- Image View
6) Go to MyCell.h and implement following custom properties

@property (weak, nonatomic) IBOutlet UILabel *prdName;
@property (weak, nonatomic) IBOutlet UILabel *prdPrice;
@property (weak, nonatomic) IBOutlet UILabel *prdDes;
@property (weak, nonatomic) IBOutlet UIImageView *prdImage;


7) Go to tableviewcontroller, import MyCell.h
    #import "mycell.h"

8) Go to controller.m and go to method "CellForRowAtIndexPath" and remove the code of default cell, implement custom cell by following
- (void)viewDidLoad {
    
    idevices=[NSArray arrayWithObjects:@"iPhone",@"iPad",@"iPod",@"iPadmini", nil];
    androidDevices=[NSArray arrayWithObjects:@"HTC",@"samsung",@"Lenevo",@"Nexus", nil];
    
    idevicesImg=[NSArray arrayWithObjects:@"1.jpeg",@"2.jpeg",@"3.jpeg",@"4.jpeg", nil];
    andImg=[NSArray arrayWithObjects:@"11.png",@"22.png",@"33.jpeg",@"44.jpeg", nil];

    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#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";
//    if (indexPath.section==0) {
//        cell.textLabel.text=[idevices objectAtIndex:indexPath.row];
//    }
//    else
//    {
//        cell.textLabel.text=[androidDevices objectAtIndex:indexPath.row];
//    }
    
      static NSString *simpleTableIdentifier = @"MyCellID";
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // Configure Properties
    if (indexPath.section==0) {
        cell.prdName.text=[idevices objectAtIndex:indexPath.row];
        cell.prdPrice.text=@"20000";
        cell.prdDes.text=@"This is an apple device";
        cell.prdImage.image=[UIImage imageNamed:[idevicesImg objectAtIndex:indexPath.row]];
    }
    else{
        cell.prdName.text=[androidDevices objectAtIndex:indexPath.row];
        cell.prdPrice.text=@"10000";
        cell.prdDes.text=@"This is an Android device";
//        NSLog(@"height is %f", cell.prdImage.frame.size.width);
//        cell.prdImage.frame.size.width== cell.prdImage.frame.size.width-30;
//        NSLog(@"height is %f", cell.prdImage.frame.size.width);
//
        cell.prdImage.image=[UIImage imageNamed:[andImg objectAtIndex:indexPath.row]];
        
    }
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115.0;
}





9) MyCell.Xib ---> Select CustomCell ---->identifier as "MyCellID"



10)
11) Build and Run the Application


No comments:

Post a Comment