Thursday, 20 February 2014

iPhone Development:Add image to UITableViewCell

Working with TableView is a regular process while creating apps for mobile platform.In this post i am going to tell you how to add image to a cell of tableview. As you all know a tableview in iPhone consist of cell.To draw a cell we will use data source method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

We will write code that customize our cell layout in this method.
For the simplest you can set image to a cell by simply writing off the given below line
cell.imageView.image = [UIImage imageNamed:@"image.png"];
Note:- this should only work for cells with style UITableViewCellStyleDefault which is the default style. For other styles, you'd need to add a UIImageView to the cell's contentView.
To add image to your content view you can use  below code, its simple create an UIImageView instance then give it frame and image.In the end add it to cell subview.
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"arrow2.png"];
[cell.contentView addSubview:imv];
[imv release];

 I simply gave an example for image you can customize or place and UI on cell
as per your need


No comments:
Write comments