2016-06-15 3 views
0

cellForRowAtIndexPath에서 각 셀의 높이를 계산하는 테이블 뷰가 있으며 heightForRowAtIndexPath는 결과를 사용합니다.reloaddata 테이블 뷰 위치가 잘못되었습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
      static NSString *CellIdentifier = @"PhotoCell"; 
      UITableViewCell *sectionHeaderView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (sectionHeaderView == nil) 
    { 
       sectionHeaderView = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
//setting up the cell 
and also calculate the total height required for this cell and save to _eachcellheight 
} 

그 세포를위한 부분.

- (void)viewWillAppear:(BOOL)animated { 
CGPoint offset = tableView.contentOffset; 
    [tableView reloadData]; 
    [tableView setContentOffset:offset]; 
} 

보기에서 테이블의 데이터를 다시로드해야합니다. 옵션을 하나의 시작에서 테이블 디스플레이를 사용하는 것은 괜찮을 것입니다하지만 테이블의 오프셋 (offset) reloaddata 후 이상한 가서 스크롤하는 것도 이상한 갈 경우

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return _eachcellheight; //option 1 
    return 200; //option 2 
} 

이제 문제입니다.

하지만 옵션 2를 사용하면 각각 200.f를 반환하므로 reloaddata는 정상적으로 작동하며 이전과 같은 위치에 머무를 수 있습니다. 그러나 표의 셀 높이가 달라야합니다.

어떻게 해결할 수 있습니까? 3 일 동안이 문제를 해결했습니다.

답변

0

cellForRowAtIndexPath 전에 heightForRowAtIndexPath이 호출되기 때문입니다. 높이 계산 논리를 cellForRowAtIndexPath 대신 heightForRowAtIndexPath 안에 넣으면 제대로 작동합니다. 표보기는 셀을 실제로 만들기 전에 각 셀의 높이를 알아야합니다.

+0

그러나 각 셀의 높이는 cellForRowAtIndexPath의 모든 데이터에 대한 기준을 계산해야합니다. 나는 움직일 수 없다. 사용할 준비가 된 배열과 높이를 같게 할 수 있습니까? 그럴 수 있니? –

관련 문제