2014-09-26 85 views
0

我想實現一個表格視圖,當我滾動頂部單元格的高度變得比其他單元格更大時。 我基本上想要第一個單元總是200f,其餘的都是100f。 我能夠做到這一點,當用戶向下滾動,但當用戶滾動到頂部,滾動速度太快,框架沒有正確重置。如何使用contentOffset更改uitableviewcell的框架高度

初步認識代碼:

- (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

試試這個!

- (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

這根本就沒什麼區別......第一個單元不需要任何調整。 問題在於,當用戶滾動過快時,不會爲每個連續偏移量更改調用方法,因此框架不會重置爲原始大小。 – 2014-09-26 15:55:17

+0

當您回到頂部或雙向滾動時,是否出現問題?希望這有助於更新。 – Bejibun 2014-09-26 16:33:53

+0

問題是當我在任一方向上滾動速度過快時,當我滾動回頂部時大多可見,因爲由於每次連續偏移更改都不會調用didscroll方法,所以展開後的tableviewcell無法完全重置爲其原始大小。當indexPath.row爲零時將內容偏移重置爲0沒有意義 – 2014-09-26 16:44:28

0

迭代通過可見細胞中並在viewDidScroll的端重置其幀奮力排除必須被轉換,即在屏幕上的第一個可見100.0f小區的小區。

[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 
    } 


}]; 
相關問題