UIImageView inside a UITableViewCell

I'm trying to display a UIImageView inside a UITableViewCell.

Inside method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I have:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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


    CGSize cellSize = cell.frame.size;
    CGFloat cellWidth = cellSize.width;
    CGFloat cellHeight = cellSize.height;

    CGRect imageFrame = CGRectMake(0, 0, 70, cellHeight);
    UIImageView * image = [[UIImageView alloc] initWithFrame:imageFrame];
    [image setImage:[UIImage imageWithContentsOfFile:@"cyan.jpg"]];

    CGRect nameFrame = CGRectMake(80, 0, cellWidth-80, cellHeight/2);
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
    nameLabel.text = @"Name: John Doe";

    CGRect jobFrame = CGRectMake(80, 20, cellWidth-80, cellHeight/2);
    UILabel * jobLabel = [[UILabel alloc] initWithFrame:jobFrame];
    jobLabel.text = @"Job: IT-Consultant";

    [cell addSubview:image];
    [cell addSubview:nameLabel];
    [cell addSubview:jobLabel];

    return cell;
}

The labels are displaying perfectly but I don't see the image.

Any help would be appreciated.

Answers


imageWithContentsOfFile: expects a path to be sent to it. Use imageNamed: instead, or get hold of the path first by using NSBundle's pathForResource... methods first to get the path.

Also, if the image is the same for every cell, you should add the image view inside the cell=nil block, or you will be adding the view over and over again.


Need Your Help

Create a simple shopping cart in wordpress?

php wordpress shopping-cart e-commerce

I have a wordpress website, and I want to create a simple web commerce. When I mean simple, it's really simple: Some posts/pages are going to have a custom field named "price". These pages are goin...

What does WindowsIdentity.GetCurrent().Impersonate() do

c# .net

I am writing a class to handle impersonation and delegation for use in in asp.net, WCF services, and WinForms apps.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.