2012-06-25 52 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上訪問它,並相應地調整了框架。現在它滾動順利:) –