如何在uiTableviewCell中获取下一个indexPath

时间:2012-10-10 12:05:54

标签: objective-c ios uitableview nsfetchedresultscontroller

我想用5种不同的图像视图填充我的自定义tableview单元格。 在我的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。我现在的问题是如何做到这一点?

亲切的问候。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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];

因此,您可以得到您所要求的:在单元格中每个位置上的相同图像。如果您想在每个单元格中使用不同的图像,则需要为六个不同的播放器图像获取六个不同的链接,然后您需要下载这些图像并将每个图像设置到单元格中的正确位置。