2014-09-26 2 views
0

상단의 셀 높이를 스크롤 할 때 나머지 셀보다 커지는 테이블 뷰를 구현하려고합니다. 기본적으로 첫 번째 셀은 항상 200f이고 100f가되어야합니다. 사용자가 아래로 스크롤 할 때 할 수 있지만 사용자가 위쪽으로 스크롤하고 너무 빠르게 스크롤하면 프레임이 제대로 재설정되지 않습니다.contentOffset을 사용하여 uitableviewcell의 프레임 높이를 변경하는 방법

relavant 코드 :

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 

    int row = scrollView.contentOffset.y/kSmallHeight; 
    _currentCellIndexPath = [NSIndexPath indexPathForRow:row inSection:0]; 
    UITableViewCell *topCell = [self.tableView cellForRowAtIndexPath:_currentCellIndexPath]; 
    NSIndexPath *path = [NSIndexPath indexPathForRow:_currentCellIndexPath.row + 1 inSection:_currentCellIndexPath .section]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path]; 

    [self.tableView insertSubview:cell aboveSubview:topCell]; 
    [self adjustCellHeightForCell:cell withDefaultOffset:NO]; 
} 

- (void)adjustCellHeightForCell:(UITableViewCell *)cell withDefaultOffset:(BOOL)defailtOffset 
{ 
    int row = self.tableView.contentOffset.y/kSmallHeight; 

    double fakeOriginy = (kBigHeight + kSmallHeight * (cell.tag-1)); 
    double set = row * kSmallHeight; 
    CGRect frame = cell.frame; 
    double offset; 

    if(defailtOffset) 
     offset = kSmallHeight; 
    else { 
     offset = ((self.tableView.contentOffset.y - set) * ((kBigHeight - kSmallHeight)/ kSmallHeight)); 
    } 

    frame.origin.y = fakeOriginy - offset; 
    frame.size.height = kSmallHeight + offset; 


    cell.frame = frame; 

} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    int row = self.tableView.contentOffset.y/kSmallHeight; 
    _currentCellIndexPath = [NSIndexPath indexPathForRow:row inSection:0]; 
    if(indexPath.row != 0 && indexPath.row < _currentCellIndexPath.row) 
     [self adjustCellHeightForCell:cell withDefaultOffset:YES]; 
} 

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    //This is the index of the "page" that we will be landing at 
    NSUInteger nearestIndex = (NSUInteger) ((int)targetContentOffset->y % (int)kSmallHeight) >= kSmallHeight/2 ? targetContentOffset->y/kSmallHeight + 1 : targetContentOffset->y/kSmallHeight; 
    //Just to make sure we don't scroll past your conpo tent 
    nearestIndex = MAX(MIN(nearestIndex, self.store.coupons.count - 1), 0); 

    //This is the actual x position in the scroll view 
    CGFloat yOffset = nearestIndex * kSmallHeight; 

    //I've found that scroll views will "stick" unless this is done 
    yOffset = yOffset==0?1:yOffset; 

    //Tell the scroll view to land on our page 
    *targetContentOffset = CGPointMake(targetContentOffset->x,yOffset); 

} 

답변

0

이것 봐! 즉, 표시 화면에 제 100.0f 세포 형질 전환되어야하는 셀을 제외한 작업 한 표시 셀 반복 적용 및 viewDidScroll의 끝에서 그 프레임을 재설정

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
{ 
    if(indexpath.row == 0) 
    { 
     //call your re-adjust height method here 
     [your_table setContentOffset:CGPointZero animated:YES]; 
     [self adjustCellHeightForCell:cell withDefaultOffset:NO]; 
    } 
} 
+0

이것은 전혀 차이가 없습니다. 첫 번째 셀은 어쨌든 조정할 필요가 없습니다. 문제는 사용자가 너무 빠르게 스크롤하면 모든 연속적인 오프셋 변경에 대해 메서드가 호출되지 않으므로 프레임이 원래 크기로 다시 설정되지 않습니다. –

+0

맨 위나 양방향으로 다시 스크롤 할 때 문제가 발생합니까? 바라기를 이것은 도움 받 ㄴ, 새롭게했다. – Bejibun

+0

문제는 두 방향으로 너무 빠르게 스크롤하지만 확장 된 tableviewcell이 모든 연속적인 오프셋 변경에 대해 호출되지 않는 didscroll 메서드로 인해 원래 크기로 완전히 다시 설정할 수 없기 때문에 맨 위로 스크롤 할 때 주로 표시되는 경우입니다. indexPath.row가 0 일 때 콘텐츠 오프셋을 0으로 재설정하면 의미가 없습니다. –

0

.

[visibleCells enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL *stop) { 

    NSIndexPath *path = [self.tableView indexPathForCell:cell]; 

    if(path.row > topCellPath.row + 1) { 

     // reset frames of small cells 

    } 
    else if(path.row <= topCellPath.row){ 

     // reset frames of big cells 
    } 


}]; 
관련 문제