0

UILabelsセルの contentView プロパティを使用してテーブル ビュー セルに表示されないという問題があります。セルにUIImageUILabelsを追加する前に表示されていました。セルにテキストを追加したいと思い、 cell.contenView を使用して、必要な場所に UILAbels を配置できるようにしました。

これを手伝ってくれてありがとう。

ここに私のコードがあります:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellReuseIdentifier = @"cellReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];


        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];

        titleLabel.tag = 234234;
        titleLabel.backgroundColor = [UIColor clearColor];

        titleLabel.textColor = [UIColor whiteColor];
        [titleLabel setFont:[UIFont fontWithName:@"DIN" size:24]];
        titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
        titleLabel.layer.shadowOpacity = 0.7;
        titleLabel.layer.shouldRasterize=YES;

        [cell.contentView addSubview:titleLabel];

        UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];

        detailLabel.tag = 345345;
        detailLabel.backgroundColor = [UIColor clearColor];

        detailLabel.textColor = [UIColor whiteColor];
        [detailLabel setFont:[UIFont fontWithName:@"DIN" size:18]];
        detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
        detailLabel.layer.shadowOpacity = 0.7;
        detailLabel.layer.shouldRasterize=YES;

        [cell.contentView addSubview:detailLabel];

    }


    UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234];
    titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];

    UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345];
    detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"description"];


    NSString *imageUrlString = [NSString stringWithFormat:@"%@", [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"]];
    UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString];
    if (cachedImage)
    {
        cell.imageView.image = cachedImage;
    }
    else
    {

        cell.imageView.image = [UIImage imageNamed:@"rest.jpg"]; // initialize the image with blank image as a placeholder

        // download in the image in the background

        [self.imageDownloadingQueue addOperationWithBlock:^{

            NSURL *imageUrl   = [NSURL URLWithString:imageUrlString];
            NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
            UIImage *image    = nil;
            if (imageData)
                image = [UIImage imageWithData:imageData];

            if (image)
            {

                [self.imageCache setObject:image forKey:imageUrlString]; // add the image to your cache

                // finally, update the user interface in the main queue

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    // make sure the cell is still visible

                    UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
                    if (updateCell)
                        cell.imageView.image = image;
                }];
            }
        }];
    }

    return cell;
}

cell.textLabel.text = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"]; また、 contentView を使用する代わりに追加 すると、セル テキストが右側に表示されます。

ここに画像の説明を入力

4

1 に答える 1