Monday, April 22, 2013

Load Distinct Images from array into UITabeView


Load Distinct Images from array into UITabeView


Step 1 
 Create Project (UITableView)  by using this tutorial


Step 2
 Add images to your project









Step 3

Goto the  " .h " file of you
change the code like this


//  ViewController.h
//  ImageTableviewExample
//
//  Created by Apple on 4/22/13.
//  Copyright (c) 2013 Dhanushka Adrian. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
   
    NSArray *tableData;       //variable for holding the table data
    NSArray *tableImageData;  //variable for holding the images
   
   
}

@end

Step 4

Goto " .m " file
change the code like this


//
//  ViewController.m
//  ImageTableviewExample
//
//  Created by Apple on 4/22/13.
//  Copyright (c) 2013 Dhanushka Adrian. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tableData,tableImageData;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    // Initialize table data
    tableData = [NSArray arrayWithObjects:@"SLIIT - Malabe", @"SLIIT - Kollupitiya", @"SLIIT - Matara",nil];
    tableImageData=[NSArray arrayWithObjects:@"images1.jpeg", @"images2.jpeg", @"images3.png",nil];
   
   
      
}

Step 5
Add following two methods  to ".m" 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
   
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
   
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[tableImageData objectAtIndex:indexPath.row]];
   
    return cell;
}

Step 6
 Run your App



Description





Characteristic of cell object















The UITableViewCell class defines three properties for this cell content:
·       textLabel—A label for the title (a UILabel object)
·       detailTextLabel—A label for the subtitle if there is additional detail (a UILabel object)
·       imageView—An image view for an image (a UIImageView object)




    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[tableImageData objectAtIndex:indexPath.row]];





No comments:

Post a Comment