Posts Tagged ‘uiimageview’

iPhone OS SDK – Tableview Text and Subtitle with Image using Indent

April 6th, 2010 iPhone SDK Tips

Today I am posting something small which adds a great touch to a UITableViewCell. I would like to show you how to add an image to the left of a tableview cell, in the minimum amount of code (realistically) possible.

Firstly I am using the TableViewStyle UITableViewCellStyleSubtitle Which is great for a main row of text and a smaller row of text underneath.

UITableViewCellStyleSubtitle – Subtitle Row Style

First the visual output:
Screenshot2010-04-01at20.18.37.gA30siNRxbKg.jpg

and the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”CellIdentifier”;

// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

}

        cell.detailTextLabel.text = tempName;        
        cell.textLabel.text = tempProject;

}

But I would like to add an image to the left side. In the past I have Created three custom views ontop of each cell, and then for each cell, set a number of properties and inserted the text/image. This was all created and deallocated automatically.

I have found an even simpler way to do all of this. It uses the fantastic property ‘indentationWidth

UITableViewCellStyleSubtitle – Subtitle Row Style with Image

First the output pic:

ss2.tCfIZR4wjSgz.jpg

And the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”CellIdentifier”;

// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                
                //Indentation Code.
                cell.indentationLevel = 1;
                cell.indentationWidth = 30;
                
                CGRect frame; frame.origin.x = 5; frame.origin.y = 5; frame.size.height = 32; frame.size.width = 32;
                
                //Print Icon
                UIImageView *imgLabel = [[UIImageView alloc] initWithFrame:frame];
                imgLabel.tag = 1000;
                [cell.contentView addSubview:imgLabel];
                [imgLabel release];        

}

        cell.detailTextLabel.text = tempName;        
        cell.textLabel.text = tempProject;

        //Setup Pic in Cell
        UIImageView *imageView = (UIImageView *) [cell.contentView viewWithTag:1000];
        imageView.image = [UIImage imageNamed:@"ico-project.png"]; // set the image for the imageview

}

This is a vast improvement to what I have previously used and have seen online.

I Hope this is found to be useful!

iPhone SDK FirstRun.png – Displaying Special ‘one off’ info in an app

February 19th, 2010 iPhone SDK Tips

In making MicroCRM, I wanted to display some temp information to the user when a table list was empty. I wanted to make it as clear as possible a ‘lead’, ‘job’ or ‘invoice’ needs to be added first.

This is not a new concept in iPhone/Mac apps, but I want to share my implementation.

1. Firstly, I made the picture I wanted to display in my app.  Then it was added to my Xcode project. Below is the actual artwork the app uses.

2. The following code is placed in  (void)viewWillAppear: for the ViewController I wanted to place the image. (I have commented it well to help the reader).

//de.009 - FirstRun Image if ArrayCount is 0

[firstRunImage removeFromSuperview];

int InvoiceCount = [appDelegate.Array count];    //get number of Invoices<br />
NSString *arraycount = [NSString stringWithFormat:@"%d", InvoiceCount];<br />
NSLog (@"Leads Page Loaded: %@ Entries",arraycount);    //Log code for number of items in array


if (InvoiceCount==0) {       //If there are no invoices<br />
NSLog (@"Adding First Run Image");     //Log code<br />
firstRunImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img-FirstRun.png"]];     //Allocate UIImageView to hold the image, and define the image<br />
firstRunImage.frame = CGRectMake(0,0,320,100);      //Define Frame Size<br />
[self.view addSubview:firstRunImage];    //Add ImageView to current view<br />
} else {<br />
[firstRunImage removeFromSuperview];    //Otherwise remove it<br />
[firstRunImage release], firstRunImage=nil;  //release memory<br />
}

See it in action

And thats it. I have the same code for all three sections of my app. To get the app, goto http://microcrmapp.com and follow the app store link.

Other implementations of a similar idea are found in many different apps. Apple’s own included apps, the facebook app and so on. There is a particularly common UI element that seems to be an imitation of apple’s own included ‘no data/history/updating’ page (see below). This is becoming more and more common place and it does seem to be a nice way to show the user ‘theres nothing!’.

I have even found a way to implement this, its using the amazing TapkuLibrary. I’ll let you work it all out for yourself now and it may even become a new post in the future

iPhoneOS iPhone iPad Development (quick) Displaying an Image in UIImageView

February 10th, 2010 iPhone SDK Tips

A quick iPhoneOS Development tip today, loading an image resource into a UIImageView field.

I had created a UIImageView in Interface Builder, but I wanted the image to change every time the page appears.

In interface builder I have linked a UIImageView to the IBOutlet recipeImage

I use the following code to load up a new image:

//Load Image <br />
NSString *recipeImageFileName = [temperatureDictionary objectForKey:@"Image"];   //Get Image Name as String (no extension) <br />
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:recipeImageFileName ofType:@"jpg"]]; //load the image into memory, adapt the parameters (filename, extension) as per needs <br />
[recipeImage setImage:img];  //show the image in the UIImageView


Super simple!