2013-05-17 2 views
1

그룹화 된 셀이있는 테이블 뷰가 있습니다. 이 셀 중 하나에 이미지가 포함되기를 원합니다. 여기에 이미지를 삽입하고 셀에 맞게 만들어 내 코드입니다 :tableViewCell에 이미지를 맞출 수 없습니다.

   logoCell = [tableView dequeueReusableCellWithIdentifier:LogoCellIdentifier]; 
      if (logoCell == nil) { 
       logoCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LogoCellIdentifier]; 
      } 

      UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, logoCell.frame.size.width, 80)]; 
      [imgView setImage:image]; 

      [logoCell.contentView addSubview:imgView]; 

을하지만있는 tableView가 표시 될 때 내 이미지는 셀의 폭보다 크다. 어떻게하면 세포에 맞출 수 있습니까?

답변

2

이미지를 tableViewCell의 배경색으로 추가하면 멋진 둥근 모서리가 나타납니다. 그렇지 않으면 그룹화 된 셀이 이미지를 마스크하지 않습니다. UIImageView의 contentMode를 설정하여 모든 것을 셀에 맞게 조절할 필요가 있습니다.

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, logoCell.frame.size.width, 80)]; 
imgView.contentMode = UIViewContentModeScaleAspectFit; 
cell.backgroundColor = [UIColor colorWithPatternImage:imgView.image]; 
+0

감사합니다. 정확히 내가 원하는 것입니다. – louftansa

0

UIImageView이 방법을 만들기 :

UIImageView *imgView = [[UIImageView alloc] initWithImage:image]; 
imgView.frame = CGRectMake(0, 0, logoCell.frame.size.width, 80); 

사이드 주 - 셀에 새로운 UIImageView 때마다 추가하지 않는 확인하십시오. 한 번만 추가하려고합니다. 스크롤 할 때 셀은 다시 사용됩니다. 코드를 구현하는 방법에 따라 여러 개의 이미지 뷰를 셀에 쉽게 추가 할 수 있습니다.

+0

메모를 보내 주셔서 감사합니다. – louftansa

1

이미지보기를 UITableViewCell에 추가하는 방법은 이미지보기로 수행하려는 작업에 따라 다릅니다. 이미지를 셀의 내용에 포함 시키려면 셀 작성시 이미지를 추가하십시오.

logoCell = [tableView dequeueReusableCellWithIdentifier:LogoCellIdentifier]; 
if (logoCell == nil) { 
    logoCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LogoCellIdentifier]; 

    // ADD IMAGEVIEW ONLY WHEN CREATING CELL 
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, logoCell.frame.size.width, 80)]; 
    [logoCell.contentView addSubview:imgView]; 

    // DONT ALLOW IMAGE OVERFLOW 
    imgView.clipsToBounds = YES; 
} 

// SET YOUR IMAGE EVERY TIME 
[imgView setImage:image]; 

당신은 당신이 tableView:willDisplayCell:forRowAtIndexPath에서 셀의 backgroundView 속성을 설정해야합니다, 배경보기로 설정하려는 경우. 이미지보기가 셀과 동일한 크기인지 확인하십시오.

UIImageView *imgView = [[UIImageView alloc] initWithFrame: cell.bounds]; 
imgView.image = image; 
cell.backgroundView = imgView; 
관련 문제