2012-06-25 55 views
2

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

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

} 

有什么建议吗?

+0

IS影像被正确的对象中初始化?可能需要看一些更多的代码,因为你显示的代码看起来很好。 – JulenissensHjelper

+0

@ K.Hole我已添加更多代码,看看 –

+0

您在哪里创建单元格? –

回答

2

您错过了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子视图造成的。我不确定故事板如何处理单元格的重用,但通常情况下,您的类将具有标签ivar或常量,以跟踪要在单元格内重新使用的视图的标记。作为例子更新我的代码。

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

我用你的代码,它总是跳转到其他部分,imageView永远不会被初始化。 –

+0

问题是您正在使用故事板。单元格是从故事板界面文件加载的,因此从不为零。我为故事板添加了一个可以工作的备用功能。 – Dima

+0

谢谢你,但我昨天得到它的工作,我做了一个自定义的单元格,里面有一个imageVIEW插座,在cellForRow上访问它,并相应地调整了框架。现在它滚动顺利:) –