크기가 다른 이미지가있는 셀에 ReusableCell을 사용하려고합니다. 이미지는 배율이 UIViewContentModeScaleAspectFit
인 220x150 블랙 박스에 넣습니다.다른 크기의 이미지가있는 UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]];
[cell.imageView setImage:[[UIImage alloc] initWithData:data]];
[cell.imageView setBackgroundColor:[UIColor blackColor]];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect imageViewFrame = cell.imageView.frame;
imageViewFrame.size.width = 220;
imageViewFrame.size.height = 150
[cell.imageView setFrame:imageViewFrame];
[cell.textLabel setText:item.title];
return cell;
}
위의 코드는 다음과 같은 레이아웃을 나타내며 테이블보기에서 스크롤 할 때 이미지가 변경되는 경우가 있습니다. 대신이 구조화되지 않은 레이아웃의
, 내가하고 싶은 이미지는 다음과 같이 정렬합니다 :
은 무엇 나는이 ReusableCell으로 잘못하고 있는가?
EDIT1 :
나는 이미지 뷰를 만들고 cell.contentView에 수퍼로이 이미지 뷰를 추가하기 위해 노력하고있어. 다음에
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NewsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *placeholderImage = [UIImage imageNamed:@"ImagePlaceholderThumb"]; //220x150
UIImageView *imageView = [[UIImageView alloc] initWithImage:placeholderImage];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]];
[imageView setImage:[[UIImage alloc] initWithData:data]];
[imageView setBackgroundColor:[UIColor blackColor]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect imageViewFrame = imageView.frame;
imageViewFrame.size.width = placeholderImage.size.width;
imageViewFrame.size.height = placeholderImage.size.height;
[imageView setFrame:imageViewFrame];
[cell.contentView addSubview:imageView];
[cell.textLabel setText:item.title];
return cell;
}
상기 코드 결과
이는 이미지의 일부처럼, 두 세포에서 볼 수있다. 그들은 내가 설정 한 크기를 유지하지 않는 것 같습니다 imageViewFrame. 왜 그런지 알아?
당신이 찾고있는 무엇이 당신이 그것을하면 않는 한 UIKit는 당신을 위해하지 않을 무언가 인 편지 복싱 및 AFAIK에게 불린다. – Till
그럼 어떻게 할 수 있습니까? – dhrm
내 대답에 대한 의견보기. 또한, 제쳐두고, UIImageView를 만들고 데이터를로드하는 방법이 셀이 화면에서 볼 수있게 될 때마다 발생합니다. 스케치 스크롤을 볼 수 있습니다. 0) 이미지 데이터를 비동기 적으로로드하고 EGOImageView와 같은 캐시를 사용합니다. 1) 일반적으로 셀을 만들 때 셀의 하위 뷰를 만들려고합니다. 셀을 만들 때 하위 뷰의 내용을 변경하려는 경우// 셀 구성 ... – Keller