UIImageView不会出现在自定义UITableViewCell中

时间:2011-12-28 16:35:04

标签: uitableview uiimageview

我需要在tableview中每行显示三个图像。我创建了一个自定义的tableviewcell,其中包含三个imageview。 tableView在其cellForRowAtIndexPath:方法中将图像的名称提供给单元格,并且单元格应该将它们并排连续加载。但是图像没有出现在tableView中。我不确定为什么它不起作用。请帮忙!

以下是代码:

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

     static NSString *CellIdentifier = @"Cell";
    customCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSString *imageForCell = [arrayImages objectAtIndex:(indexPath.row*3)];
    cell.firstImageName = imageForCell;
    imageForCell = [arrayImages objectAtIndex:(indexPath.row*3)+1];
    cell.secondImageName = imageForCell;
    cell.thirdImageName = [arrayImages objectAtIndex:(indexPath.row*3)+2];
    return cell;
}

customCell.m:

#import "customCell.h"

@implementation customCell
@synthesize firstImageView, secondImageView,thirdImageView, firstImageName, secondImageName, thirdImageName;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        //Make the Rects for the images
       CGRect rectForFirstImage = CGRectMake(10, 0, 90, 140);
        CGRect rectForSecondImage = CGRectMake(110, 0, 90, 140);
        CGRect rectForThirdImage = CGRectMake(210, 0, 90, 140);

        //create the Image views with the Rects
        firstImageView.frame = rectForFirstImage;
        secondImageView.frame = rectForSecondImage;
        thirdImageView.frame = rectForThirdImage;


        //set the images
        [firstImageView setImage:[UIImage imageNamed:firstImageName]];//tried setting the image this way also
      //  firstImageView.image = [UIImage imageNamed:firstImageName];
        secondImageView.image = [UIImage imageNamed:secondImageName];
        thirdImageView.image = [UIImage imageNamed:thirdImageName];

        // set the content mode for the image views to fit in the images
        firstImageView.contentMode = UIViewContentModeScaleAspectFit;
        secondImageView.contentMode = UIViewContentModeScaleAspectFit;
    thirdImageView.contentMode = UIViewContentModeScaleAspectFit;

}
return self;
}

-(void) layoutSubviews{
    [super layoutSubviews];
    self.opaque = NO;

    [self.contentView addSubview:firstImageView];
    [self.contentView addSubview:self.secondImageView];
    [self.contentView addSubview:self.thirdImageView];
}

@end

1 个答案:

答案 0 :(得分:0)

我已经使用了以下代码来完成你的任务。我使用SDWebimage来分配图像。你可以在没有SDWebimage的情况下使用它。它正在运行试试这个

如果(indexPath.section == 0)     {

    if ([self.array count]>0)
    {
        if ([self.array count]>indexPath.row*3)
        {
            LooksObject *looksObjectRef1 = [self.array objectAtIndex:indexPath.row*3];

            [cell.firstImage setImageWithURL:[NSURL URLWithString:looksObjectRef1.looksThumbImage] placeholderImage:[UIImage imageNamed:@"no_image.png"]];



        }

        if ([self.array count]>(indexPath.row*3)+1)
        {
            LooksObject *looksObjectRef2 = [self.array objectAtIndex:(indexPath.row*3)+1];

            [cell.secondImage setImageWithURL:[NSURL URLWithString:looksObjectRef2.looksThumbImage] placeholderImage:[UIImage imageNamed:@"image.png"]];

        }

        if ([self.array count]>(indexPath.row*3)+2)
        {
            LooksObject *looksObjectRef3 = [self.array objectAtIndex:(indexPath.row*3)+2];

            [cell.thirdImage setImageWithURL:[NSURL URLWithString:looksObjectRef3.looksThumbImage] placeholderImage:[UIImage imageNamed:@"image.png"]];

        }

    }
}