设置里面只有图像(可变高度)的UITableViewCell的dynamic高度
我有一个UITableView的单元格将只包含variables高度的图像,我根据图像dynamic设置行高,它不能正常工作,滚动图像有时会重叠。这里的代码…
-(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]; }
有什么build议么?
您错过了cellForRowAtIndexPath中的代码,实际上在没有可重用的情况下创build单元格。
//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; }
编辑:错过了关于故事板的评论
编辑2:我认为这是由您创build并添加一个新的UIImageView子视图每次加载单元格。 我不确定Storyboard如何处理单元格的重用,但是一般而言,您的类将具有标签ivar或常量,以跟踪要在单元格内重新使用的视图的标记。 作为例子更新我的代码。
编辑3:下面的代码应该为故事板工作
//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; }