Add image to UITableviewCell without custom cell - iPhone | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Add image to UITableviewCell without custom cell - iPhone

Saturday

iphone - add image to UITableViewCell:
                                                             In one of my iphone app I need to display UITableView with image. The table view should look like  Image Label Disclosure so I'm using a  custom table view cell for this structure and I did it. Lately I got one solution from net to display images in UITableView without any extra custom cell view files. That means there is no custom UITableViewCell used to add an image to the left side of the cell.
            Simply configure the imageView property of the UITableView cell in your tableView:cellForRowAtIndexPath: delegate method.

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
        UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
        [myAccessoryButton setBackgroundColor:[UIColor clearColor]];
        [myAccessoryButton setImage:[UIImage imageNamed:@"arrow_right_orange.png"] forState:UIControlStateNormal];
        [cell setAccessoryView:myAccessoryButton];
        [myAccessoryButton release];
        
    }
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    NSString *imageName = [imagesList objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:imageName];
    cell.textLabel.text = [arry objectAtIndex:indexPath.row];
    
    return cell;
}


                                 

         In the above screen shot, the image name and labels are declared as array in ViewDidLoad. In the screen shot I've changed the UInavigationbar background color. and also I've added Custom-Colored Disclosure Indicators(in Orange).

How to add custom colored  Disclosure button on  UITableViewCell - iPhone:
        This is a sample code for add custom colored  disclosure button UITableViewCell. Add the follwing code  cell in your tableView:cellForRowAtIndexPath: delegate method and replace the disclosure image(arrow_right_orange.png).

 if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
        UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
        [myAccessoryButton setBackgroundColor:[UIColor clearColor]];
        [myAccessoryButton setImage:[UIImage imageNamed:@"arrow_right_orange.png"] forState:UIControlStateNormal];
        [cell setAccessoryView:myAccessoryButton];
        [myAccessoryButton release];
        
    }
 If this post is useful for you , please share this knowledge to your friends. Thanks!

2 comments:

Michael said...

The programming patterns for mobile applications.

iphone app

In the screen shot I've changed the UInavigationbar background color.

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets