将UIImageView添加到UITableViewCell?

时间:2012-05-12 18:08:38

标签: iphone objective-c ios ipad uitableview

我真的遇到了将UIImageView添加到单个UITableView单元的麻烦。在我的Storyboard中,我有一个UITableViewController和四个原型动态单元格。其中三个像普通的左细节细胞一样好,它们工作正常。但是其中一个我需要有一个UIImageView。

所以我已经将所述视图添加到单元格,给定单元格一个带有属性的自定义类,以便我可以访问图像视图。

我有以下单元格:

标题单元格 Image Cell。
URL Cell。
备注细胞。

表视图的内容会根据用户是否要添加URL,注释或图像而更改。因此,用户总是会看到标题单元格中的一个。

这是我的代码,无论出于什么原因,我都无法在该图像单元格上显示UIImageView。

- (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;
}

1 个答案:

答案 0 :(得分:-1)

为什么要更改标识符值?您需要做的就是制作自定义单元格,并在行高,检查它是否是您想要的行,并返回图像高度,否则默认大小,并在单元格行方法,再次检查所需的行并添加图像你想添加其他使得imageview无。