2014-02-20 2 views
0

내 컨트롤러이 있습니다 [postCell setPost:post];있는 UITableViewCell 사용자 정의 서브 뷰는

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"PostCell"; 

    PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    Post *post = self.displayPosts[indexPath.row]; 
    [postCell setPost:post]; 

    return postCell; 
} 

나는 그 사용하려는 내 사용자 정의 셀 모델을 보낼 수 있습니다.

- (void)setPost:(Post *)newPost 
{ 
    if (_post != newPost) { 
     _post = newPost; 
    } 
    [self setNeedsDisplay]; 
} 

이제이 버전을 사용하고 있지만 변경하고 싶습니다. 지금 나는 을 가지고 있는데, drawRect을 호출합니다. - subviews을 어떻게 사용하고 싶습니까? 그 _POST에 따라 내가 파단 값을 설정 할 IBOutlets (imageviews 이미지, labels 텍스트 등 ...) 사용하는 경우

어디서 subviews를 추가합니까?

또는 drawRect를 사용하여 buttons을 내 셀에 추가하는 방법과 이미지 및 버튼 drawRect에서 touch을 감지하는 방법을 가르쳐 주시겠습니까? 그렇게하면 현재 버전을 바꿀 필요가 없습니다.

+0

'setPost' 함수에서 뷰를 생성하고 (셀 필요에 따라) 뷰를 셀의 서브 뷰로 추가해야합니다 :'[self.contentView addSubview : newView]'. 그런 다음 셀에 포함시키려는 다른보기에 대해 다시 수행하십시오. – Putz1103

답변

1
- (void)setPost:(Post *)newPost 
{ 
    if (_post != newPost) { 
     _post = newPost; 
    } 

    [self layoutSubviews]; 
    [self refreshContent]; 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    if (self.titleLabel == nil) { 
    self.titleLabel = [[UILabel alloc] init]; 
    [self addSubview:self.titleLabel]; 
    } 

    if (self.imageView == nil) { 
    self.imageView = [[UIImageView alloc] init]; 
    [self addSubview:self.imageView]; 
    } 

    // here I also set frames of label and imageview 

} 

- (void)refreshContent 
{ 
    [self.titleLabel setText:_post.title]; 
    [self.imageView setImage:self.post.image]; 
} 

이 내가 그것을 한 적이 방식이며 UITableView의 모든 시차없이 잘 작동합니다.

0
- (void)setPost:(Post *)newPost 
{ 
    if (_post != newPost) { 
     _post = newPost; 
    } 
    //check subviews exist and set value by newPost, if need no view, add subview, if need value, set new value 
    //Following code is just sample. 
    if(!self.imageView){ 
     self.imageView = [[UIImageView alloc] init]; 
     [self.contentView addSubview:self.imageView]; 
    } 

    if(newImage){ 
     self.imageView = newImage; 
    } 

    [self setNeedsDisplay]; 
}