如何使用contentOffset更改uitableviewcell的帧高度

时间:2014-09-26 15:36:12

标签: ios objective-c uitableview uiscrollview

我正在尝试实现一个表视图,其中当我滚动顶部单元格的高度变得比其他单元格更大。 我基本上希望第一个单元格始终为200f并且休息为100f。 我可以在用户向下滚动时执行此操作,但是当用户滚动到顶部并且滚动太快时,框架无法正确重置。

相关代码:

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

}

2 个答案:

答案 0 :(得分: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];
    }
}

答案 1 :(得分: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
    }


}];
相关问题