UIImageView没有出现在自定义UITableViewCell中

问题描述:

我需要在tableview中显示每行三个图像。我创建了一个自定义tableviewcell,其中有三个图像。其cellForRowAtIndexPath:方法中的tableView将图像的名称提供给单元格,并且单元格预期将它们并排加载到一行中。但图像不出现在tableView中。我不知道为什么它不工作。请帮忙!UIImageView没有出现在自定义UITableViewCell中

下面是代码:

- (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 
+0

发现此代码无法正常工作的原因。图像中的图像将在TableView的cellForRowAtIndexPath方法中设置。所以我创建图像并将其分配给cellForRowAtIndexPath中的imageView并将其发送给单元格。这适用于每个单元显示一个图像的程度。我不知道为什么它不显示其他图像。 – user709903 2011-12-29 09:10:53

我用下面的代码为您task.I使用SDWebimage分配image.You没有SDWebimage可以使用also.Its工作试试这个

如果(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"]]; 

     } 

    } 
}