0

我想用 5 个不同的图像视图填充我的自定义表格视图单元格。在我的 customTableViewCell 中,我这样做了。

-(void)setImage:(UIImage *)image forPosition:(NSUInteger)position{
    if(position == 1){
        _img_Player1.image = image;
    }else if(position == 2){
        _img_Player2.image = image;
    }else if(position == 3){
        _img_Player3.image = image;
    }else if(position == 4){
        _img_Player4.image = image;
    }else if(position == 5){
        _img_Player5.image = image;
    }else if(position == 6){
        _img_Player6.image = image;
    }

}

在我的 CellForRowAtIndexPath 中,我这样做了。

   static NSString *simpleTableIdentifier = @"PlayerCustomCell";

    PlayerCustomCell *cell = (PlayerCustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PlayerCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    Team *team = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
    UIImage* image = [[UIImage alloc] initWithData:imageData];
    for(int i=0;i<=5;i++)
    {
        [cell setImage:image forPosition:i];

    }


    return cell;

目前我的 tableview 看起来像这样。

|---------------------------------------------------------------------|
|                                                                     |
|    bird image   bird image   bird image     bird image  bird image  |
|---------------------------------------------------------------------|
|                                                                     |
|   dog image    dog image    dog image    dog image   dog image      |
|---------------------------------------------------------------------|
|                                                                     |
|  cat image     cat image    cat image    cat image     cat image    |
|---------------------------------------------------------------------|

我希望你能看到问题所在。我认为在设置图像之后它应该转到下一个 indexPath。我的问题是现在我该怎么做?

亲切的问候。

4

2 回答 2

1

首先在.h文件中添加5个这样的NSData

  NSData *birdImgData;
  NSData *dogImgData;
  NSData *catImgData;
  NSData *aImgData;
  NSData *bImgData;

在 CellForRowAtIndexPath 中这样做:

 static NSString *simpleTableIdentifier = @"PlayerCustomCell";

PlayerCustomCell *cell = (PlayerCustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PlayerCustomCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

if(indexPath.row == 0){

Team *team = [self.fetchedResultsController objectAtIndexPath:indexPath];
if(!birdImgData){
   birdImgData = [[NSData alloc] initWithContentsOfURL:[NSURL 
URLWithString:team.image]];
   UIImage* image = [[UIImage alloc] initWithData:birdImgData];
   [cell setImage:image forPosition:1];
}

NSIndexPath *path1 = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
Team *team1 = [self.fetchedResultsController objectAtIndexPath:path1];
if(!dogImgData){
    dogImgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team1.image]];
   UIImage* image = [[UIImage alloc] initWithData:dogImgData];
   [cell setImage:image forPosition:2];
}

NSIndexPath *path2 = [NSIndexPath indexPathForRow:indexPath.row+2 inSection:0];
Team *team2 = [self.fetchedResultsController objectAtIndexPath:path2];
if(!catImgData){
   catImgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team2.image]];
   UIImage* image = [[UIImage alloc] initWithData:catImgData];
   [cell setImage:image forPosition:3];
}

NSIndexPath *path3 = [NSIndexPath indexPathForRow:indexPath.row+3 inSection:0];
Team *team3 = [self.fetchedResultsController objectAtIndexPath:path3];
if(!aImgData){
   aImgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team3.image]];
   UIImage* image = [[UIImage alloc] initWithData:aimageData];
   [cell setImage:image forPosition:4];
}

NSIndexPath *path4 = [NSIndexPath indexPathForRow:indexPath.row+4 inSection:0];
Team *team4 = [self.fetchedResultsController objectAtIndexPath:ipath4];
if(!bImgData){
   bImgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team4.image]];
   UIImage* image = [[UIImage alloc] initWithData:bimageData];
   [cell setImage:image forPosition:5];
}
}
else
{
  if(birdImgData){
    UIImage* image = [[UIImage alloc] initWithData:birdImgData];
   [cell setImage:image forPosition:1];
  }
  if(dogImgData){
    UIImage* image = [[UIImage alloc] initWithData:dogImgData];
   [cell setImage:image forPosition:2];
  }
  if(catImgData){
    UIImage* image = [[UIImage alloc] initWithData:catImgData];
   [cell setImage:image forPosition:3];
  }
  if(aImgData){
    UIImage* image = [[UIImage alloc] initWithData:aImgData];
   [cell setImage:image forPosition:4];
  }
  if(bImgData){
    UIImage* image = [[UIImage alloc] initWithData:bimageData];
   [cell setImage:image forPosition:5];
  }
} 

return cell;
于 2012-10-10T12:20:50.410 回答
0

你的代码

UIImage* image = [[UIImage alloc] initWithData:imageData];
for(int i=0;i<=5;i++)
{
    [cell setImage:image forPosition:i];

}

为单元格中的每个 imageView 设置相同的图像。此代码可以展开为类似

UIImage* image = [[UIImage alloc] initWithData:imageData];
[cell setImage:image forPosition:0];
[cell setImage:image forPosition:1];
[cell setImage:image forPosition:2];
[cell setImage:image forPosition:3];
[cell setImage:image forPosition:4];
[cell setImage:image forPosition:5];

因此,您得到的正是您所要求的:单元格中每个位置的相同图像。如果您希望每个单元格中有不同的图像,则需要为六个不同的播放器图像获取六个不同的链接,然后您需要下载这些图像并将每个图像设置在单元格中的适当位置。

于 2012-10-10T12:46:07.393 回答