0

UIImageViewを単一のUITableViewセルに追加するのに本当に問題があります。ストーリーボードには、UITableViewControllerと4つのプロトタイプ動的セルがあります。それらのうちの3つは、通常の左側の詳細セルと同じように問題なく機能します。ただし、そのうちの1つにはUIImageViewが必要です。

そこで、セルに上記のビューを追加しました。セルにプロパティを持つカスタムクラスを指定して、画像ビューにアクセスできるようにしました。

私は次のセルを持っています:

タイトルセル。
画像セル。
URLセル。
ノートセル。

テーブルビューの内容は、ユーザーが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;
}
4

1 に答える 1

-1

なぜ識別子の値を変更するのですか?必要なのは、カスタムセルを作成し、行の高さで目的の行かどうかを確認し、画像の高さまたはデフォルトサイズで戻ることです。また、行のセルで、目的の行を再度確認して画像を追加します。追加したい場合は、imageviewをnilにします。

于 2012-05-12T18:45:48.217 に答える