2013-04-23 4 views
2

UITableView에 데이터를 표시하는 데 문제가 있습니다. 나는 그것이 세포 재사용과 관련이 있다고 믿습니다. 나는 온라인에서 여기를 확인했으나 나에게 적합한 해결책을 찾지 못했습니다. 어떤 도움을 주시면 감사하겠습니다.대기열에서 대기열을 비우기위한 테이블보기 문제

텍스트 및 그림으로 채워진 배열이 있습니다. 그런 다음 tableView에 정보를 표시하고 있습니다. 정적 크기의 셀을 사용한다면 모든 것이 정상적으로 작동하지만 텍스트의 양이 변하기 때문에 heightForRowAtIndexPath 메소드를 구현했습니다. 내가 맨 아래까지 스크롤 할 때까지 이것은 잘 작동합니다.

그런 다음, 위로 스크롤하면 모든 셀 높이가 변경되고 디스플레이가 모든 뒤죽박죽이됩니다. 일부 텍스트가 잘 리거나 그림이 잘리고 일부 셀에만 텍스트의 마지막 부분 만 있습니다. 정말 세포 재사용과 관련이 있다고 생각하지만이 문제를 어떻게 공격해야할지 모르겠습니다. 아래 코드는 cellForRowAtIndexPathheightForRowAtIndexPath입니다.

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) 
{ 
    NSString *label = [_theBigArray objectAtIndex:indexPath.row]; 
    CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping]; 

    UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height +50)]; 
    textV.font = [UIFont systemFontOfSize:15]; 
    textV.text = [_theBigArray objectAtIndex:indexPath.row]; 
    textV.textColor = [UIColor blackColor]; 
    textV.editable = NO; 
    [cell.contentView addSubview:textV]; 
} 
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]) 
{ 
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 290, 100)]; 
    imageV.contentMode = UIViewContentModeScaleAspectFit; 
    imageV.image = [_theBigArray objectAtIndex:indexPath.row]; 
    [cell.contentView addSubview:imageV]; 
} 

return cell; 
[tableView reloadData]; 
} 

heightForRowAtIndexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


int rowHeight = 0.0f; 

if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) 
{  
    NSString *temp = [_theBigArray objectAtIndex:indexPath.row]; 
    CGSize size = [temp sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping]; 
    rowHeight = size.height+50; 
} 
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]) 
{ 
    rowHeight = 115.0f; 
} 

//NSLog(@"rowHeight is %i", rowHeight); 

return rowHeight; 
[tableView reloadData]; 
} 

들어 난 두 개의 서로 다른 세포를 만들어 별도로 전화를 시도했지만 같은 일이 발생합니다. 나는 여전히 동일한 heightForRowAtIndexPath 방법을 사용했습니다.

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

UITableViewCell *newCell = [[UITableViewCell alloc] init]; 

if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 

    NSString *label = [_theBigArray objectAtIndex:indexPath.row]; 
    CGSize stringSize = [label sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:NSLineBreakByWordWrapping]; 
    UITextView *textV = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height +50)]; 
    textV.font = [UIFont systemFontOfSize:15]; 
    textV.text = [_theBigArray objectAtIndex:indexPath.row]; 
    textV.textColor = [UIColor blackColor]; 
    textV.editable = NO; 
    [cell.contentView addSubview:textV]; 

    newCell = cell; 
} 
else if ([[_theBigArray objectAtIndex:indexPath.row] isKindOfClass:[UIImage class]]) 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PictureCell"]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PictureCell"]; 
    } 

    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 290, 100)]; 
    imageV.contentMode = UIViewContentModeScaleAspectFit; 
    imageV.image = [_theBigArray objectAtIndex:indexPath.row]; 
    [cell.contentView addSubview:imageV]; 

    newCell = cell; 

} 

return newCell; 
[tableView reloadData]; 
} 

아이디어가 있으십니까?

답변

2

주요 문제는 스크롤 할 때마다 셀에 하위보기를 추가하는 것이지만 셀을 다시 사용할 때 이미 하위보기가 추가 된 것입니다. 즉, 셀을 재사용하면 재사용 식별자에 따라 이미 UITextView 또는 UIImageView가 표시됩니다.

이 하위 뷰가 먼저 존재하는지 확인해야합니다. 이것은 일반적으로 -[UIView viewWithTag] 메서드를 사용하거나 UITableViewCell을 서브 클래 싱하고 각 뷰를 속성으로 할당하여 수행됩니다.

는 (당신은 내가 당신이 아웃 - 오브 - 박스 구현 더 편안 때까지있는 UITableViewCell 하위 클래스를 피할 것이다. SO viewWithTag을 사용하는 방법을 볼 수 How to get other control value from UITableViewCell? 질문 상기 좀 걸릴 수 있습니다.) 또한

코드 행을 : 당신이 첫 번째를 다시 사용할 수 있는지 확인하지 않고 새로운있는 UITableViewCell을 생성하기 때문에

UITableViewCell *newCell = [[UITableViewCell alloc] init]; 

는 오늘 컨디션이 좋습니다. 이것은 빠른 스크롤링 성능 인 셀 재사용의 모든 목적을 무효화합니다. 대신, 그냥 초기화하지 않고 선언 :

UITableViewCell *newCell; 

또한, heightForRowAtIndexPath, 당신은

  • 같이 rowHeight를 선언 있습니다 int
  • 애프터 reloadData를 호출하려고 (그것은 CGFloat가 있어야한다) 메서드가 반환됩니다 (절대 발생하지는 않지만이 메서드에서 reloadData를 호출하면 안됩니다)
+0

빠른 답변 감사합니다! 나는이 것들을 밖으로 시도하고 다시 당신에게 돌아갈 것입니다. 시간 내 주셔서 감사합니다. – Douglas

+0

감사합니다. 도움이된다면 답변을 승인으로 표시하십시오. –

+0

그래서 나는 당신이 연결 한 그 게시물을 보았습니다. 그러나 그들은 cellForRowAtIndexPath 메서드 외부에 서브 뷰를 만들고 태그를 붙인 다음 메서드에 추가합니다. 그러나 나는 그 방법으로 그들을 창조했다. 내가 그들을 어디에서 만들어야합니까? 이후 배열 요소에 대한 정보를 만들어야합니다. 나는 또한 당신의 충고에 따라 몇 가지를 바 꾸었습니다. 고마워. – Douglas

관련 문제