SwipeView
SwipeView copied to clipboard
Number of visible items bug with defersItemViewLoading
if (_defersItemViewLoading)
{
startIndex = _currentItemIndex - ceilf(x / itemWidth) - 1;
numberOfVisibleItems = ceilf(width / itemWidth) + 3;
}
//create indices
numberOfVisibleItems = MIN(numberOfVisibleItems, _numberOfItems);
Is the culprit code. Here's the use case.
- I have a list of 2 items
- I show 2 (or more) items per page.
- I do not have wrapping enabled.
- I do have defersItemViewLoading enabled.
// startIndex is 0
// numberOfVisibleItems is 2
if (_defersItemViewLoading)
{
startIndex = _currentItemIndex - ceilf(x / itemWidth) - 1;
// startIndex = -2
numberOfVisibleItems = ceilf(width / itemWidth) + 3;
// numberOfVisibleItems is 3+, more than my count
}
//create indices
numberOfVisibleItems = MIN(numberOfVisibleItems, _numberOfItems);
// numberOfVisibleItems is set back to 2.
// startIndex is still -2.
In the code following this block, it will clamp the indexes to 0, and only load view 0, leaving a space where view 1 should have been.
A possible solution I've found is to modify the code to respect wrapping in this block.
if (_defersItemViewLoading)
{
NSInteger newStartIndex = _currentItemIndex - ceilf(x / itemWidth) - 1;
if (!_wrapEnabled) newStartIndex = MAX(newStartIndex, 0);
numberOfVisibleItems += abs(newStartIndex - startIndex) + 2;
if (!_wrapEnabled && newStartIndex + numberOfVisibleItems > _numberOfItems) {
numberOfVisibleItems = _numberOfItems - newStartIndex;
}
startIndex = newStartIndex;
}
else
{
numberOfVisibleItems = MIN(numberOfVisibleItems, _numberOfItems);
}
I'm sure this logic could be cleaner than a bunch of if statements.