我有一個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];
}
有什麼建議嗎?
IS影像被正確的對象中初始化?可能需要看一些更多的代碼,因爲你顯示的代碼看起來很好。 – JulenissensHjelper
@ K.Hole我已添加更多代碼,看看 –
您在哪裏創建單元格? –