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);
}
这根本就没什么区别......第一个单元不需要任何调整。 问题在于,当用户滚动过快时,不会为每个连续偏移量更改调用方法,因此框架不会重置为原始大小。 – 2014-09-26 15:55:17
当您回到顶部或双向滚动时,是否出现问题?希望这有助于更新。 – Bejibun 2014-09-26 16:33:53
问题是当我在任一方向上滚动速度过快时,当我滚动回顶部时大多可见,因为由于每次连续偏移更改都不会调用didscroll方法,所以展开后的tableviewcell无法完全重置为其原始大小。当indexPath.row为零时将内容偏移重置为0没有意义 – 2014-09-26 16:44:28