用图像填充自定义uitableviewCell

时间:2012-10-10 14:03:34

标签: iphone objective-c ios uitableview

我正在努力实现this。 目前我有this

所以我到目前为止将图像放在我的customTableViewCell中。但它总是在每一行上放置相同的图像。我想要的是在下一行中拍摄下一张图片。

我有一个计数器,即'indexOfIndexPath'。这就是我在CellForRowAtIndexPath中所做的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    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){
     NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row+_indexOfIndexPath inSection:0];
     NSLog(@"%@",path);
     Team *team = [self.fetchedResultsController objectAtIndexPath:path];
    if(!img1){
        img1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
        UIImage* image = [[UIImage alloc] initWithData:img1];
        [cell setImage:image forPosition:1];
        _indexOfIndexPath++;
    }

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

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

    NSIndexPath *path3 = [NSIndexPath indexPathForRow:indexPath.row+_indexOfIndexPath inSection:0];
    Team *team3 = [self.fetchedResultsController objectAtIndexPath:path3];
    if(!img4){
        img4 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team3.image]];
        UIImage* image = [[UIImage alloc] initWithData:img4];
        [cell setImage:image forPosition:4];
        _indexOfIndexPath++;
    }else{
        img4 = [[NSData alloc] initWithContentsOfFile:@"keeperNil.jpg"];
        UIImage* image = [[UIImage alloc] initWithData:img4];
        [cell setImage:image forPosition:4];

    }

    NSIndexPath *path4 = [NSIndexPath indexPathForRow:indexPath.row+_indexOfIndexPath inSection:0];
    Team *team4 = [self.fetchedResultsController objectAtIndexPath:path4];
    if(!img5){
        img5 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team4.image]];
        UIImage* image = [[UIImage alloc] initWithData:img5];
        [cell setImage:image forPosition:5];
        _indexOfIndexPath++;
    }
    NSIndexPath *path5 = [NSIndexPath indexPathForRow:indexPath.row+_indexOfIndexPath inSection:0];
    Team *team5 = [self.fetchedResultsController objectAtIndexPath:path5];
    if(!img6){
        img6 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team5.image]];
        UIImage* image = [[UIImage alloc] initWithData:img6];
        [cell setImage:image forPosition:6];
        _indexOfIndexPath++;
    }
}else{
    if(img1){
        UIImage* image = [[UIImage alloc] initWithData:img1];
        if(image == nil){
            image = [image initWithContentsOfFile:@"keeperNil.jpg"];
        }else{
        [cell setImage:image forPosition:1];
        }
    }
    if(img2){
        UIImage* image = [[UIImage alloc] initWithData:img2];
        if(image == nil){
            image = [image initWithContentsOfFile:@"keeperNil.jpg"];
        }else{
            [cell setImage:image forPosition:2];
        }
    }
    if(img3){
        UIImage* image = [[UIImage alloc] initWithData:img3];
        if(image == nil){
            image = [image initWithContentsOfFile:@"keeperNil.jpg"];
        }else{
            [cell setImage:image forPosition:3];
        }
    }
    if(img4){
        UIImage* image = [[UIImage alloc] initWithData:img4];
        if(image == nil){
            image = [image initWithContentsOfFile:@"keeperNil.jpg"];
        }else{
            [cell setImage:image forPosition:4];
        }
    }
    if(img5){
        UIImage* image = [[UIImage alloc] initWithData:img5];
        if(image == nil){
            image = [image initWithContentsOfFile:@"keeperNil.jpg"];
        }else{
            [cell setImage:image forPosition:5];
        }
    }
    if(img6){
        UIImage* image = [[UIImage alloc] initWithData:img6];
        if(image == nil){
            image = [image initWithContentsOfFile:@"keeperNil.jpg"];
        }else{
            [cell setImage:image forPosition:6];
        }
    }
} 


    return cell;

任何人都知道如何解决这个问题?

提前致谢?

2 个答案:

答案 0 :(得分:1)

如果我理解正确,你想要

  • 表格0,
  • 中的图像0,1,2,3,4,5
  • 表格第1行中的图像6,7,8,9,10,1,
  • ......等等。

这应该使用以下代码:

#define IMAGES_PER_ROW  6

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    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];
    }

    NSInteger frcRow = indexPath.row * IMAGES_PER_ROW; // row in fetched results controller

    for (int col = 1; col <= IMAGES_PER_ROW; col++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:frcRow inSection:0];
        Team *team = [self.fetchedResultsController objectAtIndexPath:path];
        NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
        UIImage *image;
        if (imgData == nil) {
            // default image
            image = [UIImage imageWithContentsOfFile:@"keeperNil.jpg"];
        } else {
            image = [UIImage imageWithData:imgData];
        }
        [cell setImage:image forPosition:col];
        frcRow ++;
    }

    return cell;
}

请注意,表视图行的数量与获取的结果控制器中的行数不同,这必须在numberOfRowsInSection中考虑:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];

    NSInteger count = [sectionInfo numberOfObjects];
    return (count + IMAGES_PER_ROW - 1)/IMAGES_PER_ROW;
}

答案 1 :(得分:0)

为什么不把你的图像放在像

这样的条件下
if(indexPath.row == 0)
{
 Your first image....
}
if(indexPath.row == 1)
{
 Your second image....
}