【问题标题】:Custom tableview cell with 6 imageviews具有 6 个图像视图的自定义表格视图单元格
【发布时间】:2012-10-09 19:15:44
【问题描述】:

我有一个非常棘手的问题。这就是我的自定义单元格的样子。

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

我有一个包含 20 张图像的核心数据库。我想做的是用我的图像填充所有这些图像视图。因此,我应该有 5 行,每个图像视图中都有不同的图像。

这是我的 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;
}

这是我走了多远。我现在不知道如何正确填写这个 tableview。其他的 imageview 名称为 img_Player2,img_Player3,img_Player4,img_Player5。

有人可以帮忙吗?

谢谢

显示我想要实现的目标

This是我想要实现的:

目前我有this

【问题讨论】:

    标签: objective-c ios ipad uitableview core-data


    【解决方案1】:

    您需要将UIImageViews 添加到UITableViewCellcontentView。假设您的 5 张图片加载到名为 imagesArrayNSArray 中。为简单起见,我假设您的每张图片都是 64 (320/5) 像素宽和 44 像素高。

    cellForRowAtIndexPath 中的代码大致如下所示:

    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;
    }
    

    您的代码将是这样的:

    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;
    }
    

    【讨论】:

    • 好的,谢谢您的回答。所以不要使用自定义表格视图?
    • mm 图像显示在彼此下方并彼此重叠。我更新了我的代码
    • 是的,我只是给你一个概述。您必须修改代码以满足您的需要。图像是重叠的,因为对于每个新单元格,您都从 Core Data 获取图像。
    • 等一下,我会提供一些屏幕。
    • 你还需要清理cell.contentView,因为你正在使用cell reusing。
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多