2014-04-20 2 views
1

여기 내 코드입니다, 내 응용 프로그램에서 UITableView의 각 셀에 대해 사용자 지정 UITableViewCell을 사용하고 "heightForRowAtIndexPath"에서 셀 높이를 계산합니다. 그러나 "dequeueReusableCellWithIdentifier"를 사용하면 셀이 테이블보기를 스크롤 할 때 겹칩니다. . "dequeueReusableCellWithIdentifier"를 사용하지 않으면 사라집니다. 이 문제가 왜 발생하는지 모르시겠습니까?UITableView "dequeueReusableCellWithIdentifier"셀 내용에 기반한 동적 셀 높이의 셀 겹침 현상이 발생합니까?

실제로 "dequeueReusableCellWithIdentifier"를 사용하여 셀을 만들지는 알고 싶습니다. Tableview에 많은 셀이 표시되면 메모리 문제가 발생합니까?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
#warning Incomplete method implementation. 
    // Return the number of rows in the section. 

    return 10; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    static NSString* cellIdentifier = @"threadCell"; 
    SYGBBSTableViewCell * cell=nil; 

    //if I comment below line code , the cell overlap issue solved 
    cell = (SYGBBSTableViewCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     cell = [[SYGBBSTableViewCell alloc] initMessagingCellWithReuseIdentifier:cellIdentifier]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 





    return cell; 
} 



-(void)configureCell:(SYGBBSTableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath { 

    SYGBBSTableViewCell* ccell = (SYGBBSTableViewCell*)cell; 

    ... 
    cell.content.text=content; 

} 








- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath; 
{ 
    /// Here you can set also height according to your section and row 
    NSDictionary* thread = [MyGoController getBBSThreadAtIndex:indexPath.row]; 

    int height = [SYGBBSTableViewCell cellHeightForThreadAt:thread]; 
    return height; 
} 
+0

당신은'체크 할 필요가없는 경우 (셀 = = nil)' – meda

+0

@meda 나는 그가 dequeue 라인을 테스트 할 수 있도록 주석을 달 수 있도록 이것을한다고 생각한다. – danh

+0

@sureone은 IB에서 SYGBBSTableViewCell로 설정된 프로토 타입 셀의 클래스입니까? 그렇지 않다면 문제를 설명 할 수 있습니다. – danh

답변

0

문제는 dequeueReusableCellWithIdentifier를 사용하여 셀을 얻을 때 당신의 초기화 initMessagingCellWithReuseIdentifier 호출 할 것입니다. initMessagingCellWithReuseIdentifier에서 initWithStyle:reuseIdentifier:으로 이름을 바꾸면 제대로 작동합니다.

initWithStyle:reuseIdentifier:은 지정된 이니셜 라이저 (Doc : https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html)이며 [super initWithStyle:style reuseIdentifier:reuseIdentifier];으로 전화해야합니다. 그런 다음 구현중인 로직을 initMessagingCellWithReuseIdentifier에 구현하십시오.

0

dequeueReusableCellWithIdentifier은 셀 재사용에 유용하며 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath을 사용하여 재사용 가능한 셀을 얻지 않는 한 cell == nil을 확인해야합니다. 난 당신이 configureCell 방법과 같은 코드를 시도 할 수 있습니다, 셀 높이를 변경, 당신은뿐만 아니라 적절한 높이의 세포를 구성해야하기 때문에 문제가 configureCell 생각 :

-(void)configureCell:(SYGBBSTableViewCell*)cell atIndexPath:(NSIndexPath *)indexPath { 

    SYGBBSTableViewCell* cell = (SYGBBSTableViewCell*)cell; 
    CGFloat cellHeight = [self tableView:self.tableView heightForRowAtIndexPath:indexPath]; 

    [cell layoutSubviewWithHeight:cellHeight]; 
    [cell configureContent]; 
} 
관련 문제