如何使用UITableView显示2个UILabel和一个图像网格,也可以自动更新Cell高度

时间:2022-06-08 20:25:23

I need to build a UITableView to display a list of posts, and each post will contain 3 sections.

我需要构建一个UITableView来显示帖子列表,每个帖子将包含3个部分。

  1. Title (UILabel with 1 line of text)
  2. 标题(UILabel有1行文字)

  3. Content (UILabel with multi lines of texts)
  4. 内容(UILabel有多行文字)

  5. Grid of Images (number of images will vary from each row)
  6. 图像网格(图像数量因行而异)

I have followed this post.

我关注过这篇文章。

I am able to add the Title and Content, and with autolayout it works as I need it to. However, I cannot add the grid of images.

我可以添加标题和内容,并使用autolayout它可以正常工作。但是,我无法添加图像网格。

I have create a custom cell view class just like AutoSizeCell.h/AutoSizeCell.m in the above post. Also I have created a modal class to have three properties (title, content and NSMutableArray of image names I need to display in the grid) However, it seems I cannot pass the images names to AutoSizeCell.m, so I cannot display the image grid.

我在上面的帖子中创建了一个自定义单元格视图类,就像AutoSizeCell.h / AutoSizeCell.m一样。另外我创建了一个模态类,有三个属性(我需要在网格中显示的图像名称的标题,内容和NSMutableArray)但是,似乎我无法将图像名称传递给AutoSizeCell.m,所以我无法显示图像网格。

@implementation AutoSizeCellContents

-(id)init
{
    self = [super init];
    if (self) {
        self.images = [[NSMutableArray alloc] init];
    }
    return self;
}
@end

controller:

-(void)configureCell:(AutoSizeCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    // Configure the cell for this indexPath
    cell.category.text = [self getCategoryAtIndexPath:indexPath];
    cell.pastTense.text = [self getPastTenseAtIndexPath:indexPath];

    for (NSString *imageName in [self getImagesAtIndexPath:indexPath]) {
        NSLog(@"image name %@",imageName);
        [cell.images addObject:@"hellp"];
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Create a reusable cell
    AutoSizeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"plerp"];
    if(!cell) {
        cell = [[AutoSizeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"plerp"];
        [self configureCell:cell atIndexPath:indexPath];
    }

    return cell;
}

1 个解决方案

#1


I believe you need to override -(id)initWithCoder:(NSCoder*)aDecoder instead of init.

我相信你需要覆盖 - (id)initWithCoder:(NSCoder *)aDecoder而不是init。

Alternatively, don't do either of these things. Get rid of the init entirely and do this.

或者,不要做其中任何一件事。完全摆脱init并执行此操作。

cell.images = [self getImagesAtIndexPath:indexPath]

cell.images = [self getImagesAtIndexPath:indexPath]

Less code, less relying on cell state. And I'm pretty sure always adding images is going to cause issues when the cell gets reused unless you override prepareForReuse. Just setting the whole array is easier.

代码更少,更少依赖单元状态。而且我非常肯定,除非你覆盖prepareForReuse,否则在重复使用单元格时,添加图像会导致问题。只需设置整个阵列就更容易了。

#1


I believe you need to override -(id)initWithCoder:(NSCoder*)aDecoder instead of init.

我相信你需要覆盖 - (id)initWithCoder:(NSCoder *)aDecoder而不是init。

Alternatively, don't do either of these things. Get rid of the init entirely and do this.

或者,不要做其中任何一件事。完全摆脱init并执行此操作。

cell.images = [self getImagesAtIndexPath:indexPath]

cell.images = [self getImagesAtIndexPath:indexPath]

Less code, less relying on cell state. And I'm pretty sure always adding images is going to cause issues when the cell gets reused unless you override prepareForReuse. Just setting the whole array is easier.

代码更少,更少依赖单元状态。而且我非常肯定,除非你覆盖prepareForReuse,否则在重复使用单元格时,添加图像会导致问题。只需设置整个阵列就更容易了。