设置的UITableViewCell动力高度与仅在其内部的图像(可变高度)(Setting dyna

2019-09-18 11:28发布

我有一个UITableView它的细胞将只包含不同高度的图片,我设置的行高动态根据图像,它不完美,图像滚动时有时会overlapped.Heres代码...

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [artistfeeds count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[self.newsTableView dequeueReusableCellWithIdentifier:@"artistFeedCell"];

    UIImageView *imgView=[[UIImageView alloc]initWithImage:((ESArtistFeedObject*)[artistfeeds objectAtIndex:indexPath.section]).image];
    [cell.contentView addSubview:imgView];
    [cell.contentView sizeToFit];
    return  cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=((ESArtistFeedObject*)[artistfeeds objectAtIndex:indexPath.section]).image.size.height;
    NSLog(@"section: %i, image height: %f",indexPath.section,height);
    return height;
}

-(void)artistFeedFound:(NSNotification*)notification
{
    //NSLog(@"%@",[notification userInfo]);
    artistfeeds=[[NSMutableArray alloc]init];
    if([[[[notification userInfo]objectForKey:@"artistfeed"]objectForKey:@"artist"] isKindOfClass:[NSArray class]])
    {
        for(NSDictionary *tempDict in [[[notification userInfo]objectForKey:@"artistfeed"]objectForKey:@"artist"])
        {
            ESArtistFeedObject *artistFeed=[[ESArtistFeedObject alloc]init];
            artistFeed.name=[tempDict objectForKey:@"name"];
            artistFeed.type=[tempDict objectForKey:@"postType"];
            artistFeed.desc=[tempDict objectForKey:@"postDesc"];
            if(![artistFeed.type isEqualToString:@"photo"])
                artistFeed.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://c0364947.cdn2.cloudfiles.rackspacecloud.com/%@",[tempDict objectForKey:@"artist_image"]]]]];
            else
                artistFeed.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tempDict objectForKey:@"photo"]]]];
            [artistfeeds addObject:artistFeed];
        }
    }
    [self.newsTableView reloadData];

}

有什么建议么?

Answer 1:

你缺少的cellForRowAtIndexPath代码的时候有没有重用实际创建的细胞。

//assuming there is a class property `int cellImageTag = 100;`
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[self.newsTableView dequeueReusableCellWithIdentifier:@"artistFeedCell"];
    UIImageView *imgView;
    if(cell == nil)
    {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"artistFeedCell"];
      imgView = [[UIImageView alloc] init];
      imgView.tag = cellImageTag;
      [cell.contentView addSubview:imgView];
    }
    else
    {
      imgView = [cell.contentView viewWithTag:cellImageTag];
    }
    imgView.image = ((ESArtistFeedObject*)[artistfeeds objectAtIndex:indexPath.section]).image;
    [cell.contentView sizeToFit];
    return  cell;
}

编辑:错过了有关故事板评论

EDIT2:我认为这是通过创建和每次负荷的小区中每次添加一个新的UIImageView子视图造成的。 我不知道故事板是如何处理再利用细胞,但在一般类将有一个标签伊娃或常数来跟踪你想重新利用细胞内的意见标签。 更新我的代码作为例子。

EDIT3:下面的代码应该工作的故事板

//assuming there is a class property `int cellImageTag = 100;`
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[self.newsTableView dequeueReusableCellWithIdentifier:@"artistFeedCell"];
    UIImageView *imgView = [cell.contentView viewWithTag:cellImageTag];
    if(!imgView)
    {
      imgView = [[UIImageView alloc] init];
      imgView.tag = cellImageTag;
      [cell.contentView addSubview:imgView];
    }
    imgView.image = ((ESArtistFeedObject*)[artistfeeds objectAtIndex:indexPath.section]).image;
    [cell.contentView sizeToFit];
    return  cell;
}


文章来源: Setting dynamic height for UITableViewCell with only image (variable height) inside it