本文介绍了为UITableViewCell设置动态高度,其中只有图像(可变高度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其单元格只包含可变高度的图像,我根据图像动态设置行高,它不能很好地工作,滚动时的图像有时会重叠。这是代码......

I have a UITableView whose cells will only contain images of variable heights, I set the row height dynamically according to image, it doesnt work perfectly, the images while scrolling sometimes get overlapped.Heres the code...

-(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];

}

有任何建议吗?

推荐答案

你错过了cellForRowAtIndexPath中的代码,当没有可重用的时候,它实际上会创建单元格。

You're missing the code in cellForRowAtIndexPath that actually creates cells when there are none to reuse.

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

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

edit: missed the comment about storyboards

edit2:我认为这是由每次加载单元格时创建和添加新的UIImageView子视图引起的。我不确定Storyboard如何处理单元格的重复使用,但通常你的类会有一个标记ivar或常量来跟踪你想在单元格中重复使用的视图的标记。更新我的代码作为示例。

edit2: I think this is caused by you creating and adding a new UIImageView subview every time you load a cell. I'm not sure how Storyboards handle re-use of cells but in general your class would have a tag ivar or constant to keep track of the tag of the views you want to re-use within the cells. Updating my code as an example.

edit3:以下代码适用于故事板

edit3: The code that follows should be working for storyboards

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

这篇关于为UITableViewCell设置动态高度,其中只有图像(可变高度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:20