简体   繁体   中英

Custom tableview cell with 6 imageviews

I have a very difficult problem. This is how my custom cell looks like.

|--------------------------------------------------------------------------|
|                                                                          |
|                                                                          |
|  Imageview1   Imageview2      Imageview3     Imageview4    imageview5    |
|                                                                          |
|                                                                          |
|                                                                          |                      
|--------------------------------------------------------------------------|

I have a core database with 20 images. What I want to do is to fill up all these imageviews with my images. So at the and I should have 5 rows with in each imageview a different image.

Here is my code for my cellforrowAtIndexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    for(int i=0;i<5;i++)
    {
        float xCoord = 0.0;
        Team *team = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
        UIImage* image = [[UIImage alloc] initWithData:imageData];
        UIImageView* imgView = [[UIImageView alloc] initWithImage:image];
        [imgView setFrame:CGRectMake(xCoord,0,imgView.frame.size.width, imgView.frame.size.height)];
        [cell.contentView addSubview:imgView];
        xCoord += imgView.frame.size.width;
    }


    return cell;
}

This is how far i got. I don't now how to fill up this tableview correctly. The other imageviews are name img_Player2,img_Player3,img_Player4,img_Player5.

Can anybody help?

Thank you

SCREENS WITH WHAT I WANT TO ACHIEVE

This is what I want to achieve:

And at the moment I have this .

You need to add the UIImageViews to the contentView of the UITableViewCell . Let's assume your 5 images are loaded in an NSArray called imagesArray . For simplicity I'm assuming each of your images is 64 (320/5) pixels wide and 44 pixels high.

Your code inside cellForRowAtIndexPath should roughly look like this:

float xCoord = 0.0;
for (UIImageView *imgView in imagesArray)
{
    [imgView setFrame:CGRectMake(xCoord,0,imgView.frame.size.width, imgView.frame.size.height)];
    [cell.contentView addSubview:imgView];
    xCoord += imgView.frame.size.width;
}

Your code will be this:

float xCoord = 0.0;

for(int i=0;i<5;i++)
{
    Team *team = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
    UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];
    [imgView setFrame:CGRectMake(xCoord,0,imgView.frame.size.width, imgView.frame.size.height)];
    [cell.contentView addSubview:imgView];
    xCoord += imgView.frame.size.width;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM