iphone - 设置 UITableViewCell 的动态高度,其中仅包含图像(可变高度)

标签 iphone ios uitableview uiimageview uiimage

我有一个 UITableView,其单元格仅包含可变高度的图像,我根据图像动态设置行高,它不能完美工作,滚动时图像有时会重叠。代码如下...

-(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 中的代码,该代码在没有可重用的单元格时实际创建单元格。

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

编辑:错过了有关 Storyboard的评论

edit2:我认为这是由于您每次加载单元格时创建并添加新的 UIImageView subview 造成的。我不确定 Storyboard如何处理单元格的重用,但一般来说,您的类将有一个标签 ivar 或常量来跟踪您想要在单元格中重用的 View 的标签。以更新我的代码为例。

edit3:下面的代码应该适用于 Storyboard

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

关于iphone - 设置 UITableViewCell 的动态高度,其中仅包含图像(可变高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11189351/

相关文章:

ios - DateFormatter 不返回 "HH:mm:ss"的日期

iphone - deleteItemsAtIndexPaths 中的 UICollectionView 断言失败

ios - 填充我的 UITableView 时如何跳过特定字符串

iphone - 如何制作半透明的UITableViewCells?

ios - 使用 Objective-C 从不同的 ViewController 重新加载 TableView

ios - 申请进入应用商店后如何更改生产证书(.p12)

c# - 如何将事件推送到 iPhone 日历

ios - 如何知道最后一行标签的宽度?

iphone - 将 iPhone 通讯录联系人放入移动网络应用程序?

objective-c - iOS 对上传任务进行排队