我想用5个不同的imageview填充我的自定义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;
目前,我的表格视图如下所示。
|---------------------------------------------------------------------|
| |
| 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。我的问题是现在该怎么做?
亲切的问候。
最佳答案
首先在.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;