Search code examples
iphoneobjective-ciosipaduitableview

Adding UIImageView to UITableViewCell?


I am having real troubles adding a UIImageView to a single UITableView cell. In my Storyboard I have a UITableViewController and four prototype dynamic cells. Three of them are just fine as normal left detail cells and they work fine. However one of them I need to have a UIImageView in.

So I have added said view to the cell, given the cell a custom class with a property so I can access the image view.

I have the following cells:

Title cell.
Image Cell.
URL Cell.
Notes Cell.

The contents of the table view change depending on whether the user wants to add a URL, note or image. So the user always sees the title cell with one of the others.

Here is my code and for whatever reason I just can't get the UIImageView to display on that image cell.

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

    static NSString *CellIdentifier = @"Cell";

    //0 = Image
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 0) 
    {
        if (indexPath.row == 0) CellIdentifier = @"titleCell";
        if (indexPath.row == 1) CellIdentifier = @"imageCell";
        if (indexPath.row == 2) CellIdentifier = @"notesCell";
    }
    //1 = URL
    else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 1) 
    {
        if (indexPath.row == 0) CellIdentifier = @"titleCell";
        if (indexPath.row == 1) CellIdentifier = @"urlCell";
        if (indexPath.row == 2) CellIdentifier = @"notesCell";
    }
    //2 = Notes
    else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 2) 
    {
        if (indexPath.row == 0) CellIdentifier = @"titleCell";
        if (indexPath.row == 1) CellIdentifier = @"notesCell";
    }

    ScrapbookImageDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[ScrapbookImageDetailCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    [cell setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:250.0f/255.0f blue:250.0f/255.0f alpha:1.0f]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];

    cell.textLabel.adjustsFontSizeToFitWidth = YES;

    cell.textLabel.text = [firstSection objectAtIndex:indexPath.row];
    cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13.0f];
    cell.detailTextLabel.numberOfLines = 0;
    cell.accessoryView = nil;

    switch (indexPath.section) 
    {
        case 0:
            switch (indexPath.row) 
        {
            case 0: 
            {                
                cell.detailTextLabel.text = scrapbook.title;
            } 
                break;

            case 1: 
            {
                //0 = Image
                if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 0) 
                {
                    cell.detailTextLabel.text = nil;
                    cell.iv.image = [UIImage imageNamed:@"image.png"];
                }
                //1 = URL
                else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 1) 
                {
                    cell.detailTextLabel.text = scrapbook.url;
                }
                //2 = Notes
                else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"NewScrapbookEntry"] == 2) 
                {
                    cell.detailTextLabel.text = scrapbook.notes;
                }
            } 
            break;

            case 2:
            {
                cell.detailTextLabel.text = scrapbook.notes;
            }
            break;

            default:
                break;
        }
        break;

        default:
            break;
    }

    return cell;
}

Solution

  • Why are you changing the identifier value? The thing you just have to do is make custom cell, and in height for row, check if it's your desired row, and return with height of image else default size, and in cell for row method, again check desired row and add the image you want to add else make the imageview nil.