Showing posts with label Table View. Show all posts
Showing posts with label Table View. Show all posts

Friday, March 30, 2012

iPhone Table view with Images


Now that we have created a simple table view example we can look at extending our code to change the cell style. In so doing we will make use of the image and detailed text properties of the cell object to add images and subtitles to each row of the view. The first step is to add an image to the resources of our project. If you already have an image file that is suitable for this purpose feel free to use it in this example. Alternatively, we can pick up an image from the Mac OS X system on which Xcode is running. To add an image file to the application resources, right click on the Resources entry in the Groups and Files panel of the main Xcode project window. From the resulting menu, select the Add -> Existing Files… option. In the resulting file selection panel navigate to and select your pre-existing image file. Alternatively, enter apple.png into the search field and press Enter. The apple.png file should appear in the file list from where it can be selected:

Adding an image file to the resources of an Xcode project

Once the appropriate image file has been selected, click on the Add button to add the image to the resources. On the following panel accept the default settings and click Add once again.
Now that the image is included in the project resources it is time to add some code to the cellForRowAtIndexPath method in the TableExampleViewCrontoller.m file. All we need to do in this file is add some code to create a UIImage object from the image file and assign it to the imageView property of the cell object. With these changes made, the method should read as follows:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
        // Configure the cell.
        UIImage *cellImage = [UIImage imageNamed:@"apple.png"];
        cell.imageView.image = cellImage;
        cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];
        return cell;
}
Next, we want to add a subtitle to the text of each cell. This is achieved by assigning a string to the cell object’s detailTextLabel text property. Note also that the code currently selects the UITableViewCellStyleDefault cell style which does not display the detailText. It will also be necessary, therefore to change the cell style to UITableViewCellStyleSubtitle . With all of these changes implemented, the method now reads as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

        // Configure the cell.
        UIImage *cellImage = [UIImage imageNamed:@"apple.png"];
        cell.imageView.image = cellImage;

        NSString *colorString = [self.colorNames objectAtIndex: [indexPath row]];

        cell.textLabel.text = colorString;

        NSString *subtitle = [NSString stringWithString: @"All about the color "];
        subtitle = [subtitle stringByAppendingFormat:colorString];

        cell.detailTextLabel.text = subtitle;

        return cell;
} 
Once the changes have been made to the code, build and run the application. When the simulator starts up the table view should appear with images and subtitles as shown in the following figure:

iPhone table view with subtitles and images
References

Simple iPhone table view


Table view is commonly used to show lists of data. A table view is the view object that displays table’s data and instance of UITableView. Each visible row in a table view is implemented by UITableViewCell. Table views are not responsible for storing your table’s data. They store only enough data to draw the rows that are currently visible. Table views get their configuration data from an object that conforms to the UITableViewDelegate protocol and their row data from an object that conforms to the UITableViewDataSource protocol.
Follow the steps below to create UITableView sample:
1.Open Xcode and create new view based project named ‘SimpleTable’
2.Modify code in the SimpleTableViewController.h file as follow:
1#import <UIKit/UIKit.h>
2
3@interface SimpleTableViewController : UIViewController
4<UITableViewDelegate,UITableViewDataSource>{
5NSArray *listData;
6}
7@property(nonatomic, retain) NSArray *listData;
8@end
3.Open SimpleTableViewController.xib file and drag Table View from Library over to the View Window.
4.Connect tableview’s dataSource and delegate from connection inspector to File’s Owner.
5.Modify code in the SimpleTableViewController.m file as follow:
01#import "SimpleTableViewController.h"
02
03@implementation SimpleTableViewController
04@synthesize listData;
05
06- (void)viewDidLoad {
07NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod",
08 @"iPad",nil];
09self.listData = array;
10[array release];
11[super viewDidLoad];
12
13}
14- (void)dealloc {
15[listData dealloc];
16[super dealloc];
17}
18
19#pragma mark -
20#pragma mark Table View Data Source Methods
21
22- (NSInteger)tableView:(UITableView *)tableView
23 numberOfRowsInSection:(NSInteger)section
24{
25return [self.listData count];
26}
27
28- (UITableViewCell *)tableView:(UITableView *)tableView
29 cellForRowAtIndexPath:(NSIndexPath *)indexPath
30{
31
32static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
33UITableViewCell *cell = [tableView
34dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
35if (cell == nil) {
36cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
37reuseIdentifier:SimpleTableIdentifier] autorelease];
38}
39
40NSUInteger row = [indexPath row];
41cell.textLabel.text = [listData objectAtIndex:row];
42return cell;
43
44}
45
46@end
In viewDidLoad we are creating an array of data to pass to our Table View.
We have also added two more methods of data source delegate which are mandatory to implement when your implementing UITableViewDataSource delegate.
1(NSInteger)tableView:(UITableView *)tableView
2 numberOfRowsInSection:(NSInteger)section
which specifies how many number of rows are there in one section of the Table View.
The default number  of section in Table View is one.
Another method is
1- (UITableViewCell *)tableView:(UITableView *)tableView
2 cellForRowAtIndexPath:(NSIndexPath *)indexPath
Which is called by table view when it needs to draw a row. This method is called n times and value of n is equal to value returned by first method. As this method is called once for every row,
1if (cell == nil)
checks, if cell exits before, if not create new cell. Here ‘indexPath’ parameter gives us current Indexpath of the row from which we can get the current drawing row. Then we set the text of textLabel property of the current drawing cell and finally return the cell to the Table View.
You can download the source code used in this tutorial from here
Output will look like this: