2014-07-13 3 views
1

그래서 기본적으로 내가 사용자 지정 UITableViewCell (이 경우 UIImageView 중간에 설정된 간단한 셀이 있습니다.) 제대로 서브 클래 싱 된 (셀 및 이미지보기 .h .m 파일이 있습니다. 제대로 "퇴장했다").사용자 정의 UITableViewCell

셀을로드 할 때 UIImageView를 원으로 변경하고 UIImageView에 제스처 인식기를 추가 할 수 있어야합니다 (사용자가 ImageView를 탭하면 UIImagePicker를로드합니다. 사용자가 이미지를 설정).

내가 직면 한 문제는 코드가 디버거에 따라 실행되지만 UI가 업데이트되지 않는다는 것입니다. (여전히 일반 사각형 UIImageView가 보이고 제스처 인식기가 작동하지 않습니다.) 여기

는 cellforrowatindexpath 위임 방법에 대한 코드이다. I도 [셀 setNeedsdisplay]와 함께합니다 (willdisplaycell 위임 방법에서 동일한 코드를 넣고 있지만, 그 중 작동하지 않는 시도

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row==0 && indexPath.section==0) { 
     static NSString *CellIdentifier = @"Profile Cell"; 
     Profileviewcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[Profileviewcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     cell.ProfileImage.layer.cornerRadius = cell.ProfileImage.bounds.size.width/2; 
     cell.ProfileImage.layer.masksToBounds = YES; 
     [cell.ProfileImage setUserInteractionEnabled:YES]; 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LoadImage:)]; 
     tap.cancelsTouchesInView = YES; 
     tap.numberOfTapsRequired = 1; 
     tap.delegate = self; 
     [cell.ProfileImage addGestureRecognizer:tap]; 
     [cell setNeedsDisplay]; 
     return cell; 
    } else { 
     //return a standard cell 
    } 
} 

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row==0 && indexPath.section==0) { 
     Profileviewcell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     cell.ProfileImage.layer.cornerRadius = cell.ProfileImage.bounds.size.width/2; 
     cell.ProfileImage.layer.masksToBounds = YES; 
     [cell.ProfileImage setUserInteractionEnabled:YES]; 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LoadImage:)]; 
     tap.cancelsTouchesInView = YES; 
     tap.numberOfTapsRequired = 1; 
     tap.delegate = self; 
     [cell.ProfileImage addGestureRecognizer:tap]; 
     [cell setNeedsDisplay]; 
    } 

} 

Initwithstyle 코드 :

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     self.ProfileImage = [[UIImageView alloc] initWithFrame:CGRectMake(110,5,100,100)]; 
     [self.ProfileImage setImage:[UIImage imageNamed:@"General Profile"]]; 
     self.ProfileImage.layer.cornerRadius = self.ProfileImage.bounds.size.width/2; 
     self.ProfileImage.layer.masksToBounds = YES; 
     [self.ProfileImage setUserInteractionEnabled:YES]; 
     [self.contentView addSubview:self.ProfileImage]; 
    } 
    return self; 
} 

셀 UI가 절대로 업데이트되지 않는 이유에 대한 아이디어가 있습니까? 유사한 문제에 대해 Stack Overflow를 확인했고 제공되는 모든 솔루션이 나에게 적합하지 않은 것 같습니다. 죄송합니다. 간단한 질문 인 경우 - 최근에야 iOS 개발을 시작했습니다.

많은 감사!

+0

의 이름으로 스토리 보드 셀의 클래스를 설정 cellForRowAtIndexPath

  • nil 확인되어 제거 제스처 인식기? 그렇다면, 당신이 가지고있는 문제를 설명 할 것입니다. – bobnoble

  • +0

    @bobnoble 답장을 보내 주셔서 대단히 감사합니다! 예, 디버거에 따라 아무 것도 아니지만 정확히 이유는 모르겠습니다! 셀은 스토리 보드의 프로토 타입 셀이며 Xcode의 UITableViewCell 하위 클래스도 만들었습니다. 내가 그것을 멈추게하면 어떻게 될까요? (그리고 왜 처음부터 무의미한가요?) –

    +0

    커스텀'UITableViewCell'에 대한 코드를 보여줍니다. 특히'ProfileImage'가 선언되어있는 곳과'initWithStyle' 메소드의 일부로 어떻게 초기화되어 있는지를 보여줍니다. – bobnoble

    답변

    0

    내 문제를 발견 밝혀 나는 서브 클래스의 initwithstyle 기능에 모든 코드를 이동하는 데 필요한 가장 중요한 합성을 서브 클래스 (의 헤더에있는 UIImageView 불행하게도,이 어떤을 잊는의 신인 실수를 ImageView가 그냥 null로 남아 있음을 의미).

    같은 문제가있는 다른 사용자에게 유용 할 것입니다.

    도움을 주신 모든 분들께 감사드립니다!

    0

    수정할 때 이미지 콘센트가 nil 일 가능성이 큽니다.

    클래스 속성의 이름을 작은 문자로 시작하는 것이 관습입니다. 그렇지 않으면 클래스 이름을 실수로 쉽게 알 수 있습니다. 따라서 cell.profileImage (
    @property (nonatomic, weak) IBOutlet UIImageView *profileImage; 기준)
    이 더 적절합니다.

    또한 if (cell==nil) 블록과 setNeedsDisplay을 스크랩 할 수 있습니다.

    +0

    고마워요,이 코드를 바꿔서 알지 못했습니다! 그렇습니다. 이미지 콘센트가 nil 인 문제입니다.하지만 선언하고 인스턴스화하는 데 아무 이유가 없다는 것을 알지 못합니다. –

    +0

    Xib/Storyboard 파일에서 이미지 뷰를 확인하십시오. – Mundi

    0

    는 몇 가지가 UITableViewCell이 때문에 nil 검사가 항상 NO가 정의 셀의 initWithStyle가 호출되지 않습니다 의미 반환 유효하고 다른 사람에 의해 언급 한 바와 같이

    1. , dequeueReusableCellWithIdentifier 항상 반환 ... 여기에 무슨있다 따라서 ProfileImage은 인스턴스화되지 않으므로 에서 nil까지입니다.
    2. 현재 거기 ProfileImage 계층 수정하는 코드가있는 곳에 세 개의 장소 (cellForRowAtIndexPath, willDisplayCell, 및 사용자 정의 세포의 initWithStyle). 마지막 하나는 올바른 장소입니다.
    3. 스토리 보드에서 의심스러운 사용자 셀의 클래스는 (즉, UITableViewCell)이지만 사용자 정의 셀의 이름이어야합니다.

    는 추천 :

    • cellForRowAtIndexPath에서 ProfileImage 층의 구성을 분리 및 willDisplayCell 제거 방법. 그`layer`을 수정하고 추가 할 때
    • 는 nil``에`동일 cell.ProfileImage`을 사용자 정의 셀
    +0

    다시 한 번 감사드립니다. 귀하가 제안한 모든 변경 사항을 구현했습니다. 이 코드는 여러 가지 기능으로 코드를 복사하여 문제가 무엇인지 알아보기 위해 약간 엉성하고 반복되었습니다. UIImageView를 합성하는 것을 잊어 버렸습니다. 당신의 도움에 많은 감사드립니다! –

    관련 문제