|
1 | | -import { ItemEventData } from "."; |
| 1 | +import { ItemEventData } from "."; |
2 | 2 | import { |
3 | 3 | ListViewBase, View, KeyedTemplate, Length, Observable, Color, |
4 | 4 | separatorColorProperty, itemTemplatesProperty, iosEstimatedRowHeightProperty, layout, EventData |
5 | 5 | } from "./list-view-common"; |
6 | 6 | import { StackLayout } from "../layouts/stack-layout"; |
7 | 7 | import { ProxyViewContainer } from "../proxy-view-container"; |
8 | 8 | import { profile } from "../../profiling"; |
| 9 | +import * as trace from "../../trace"; |
9 | 10 |
|
10 | 11 | export * from "./list-view-common"; |
11 | 12 |
|
@@ -261,16 +262,31 @@ export class ListView extends ListViewBase { |
261 | 262 | } |
262 | 263 |
|
263 | 264 | public scrollToIndex(index: number) { |
264 | | - if (this._ios) { |
265 | | - this._ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), |
266 | | - UITableViewScrollPosition.Top, false); |
267 | | - } |
| 265 | + this._scrollToIndex(index, false); |
268 | 266 | } |
269 | 267 |
|
270 | 268 | public scrollToIndexAnimated(index: number) { |
271 | | - if (this._ios) { |
| 269 | + this._scrollToIndex(index); |
| 270 | + } |
| 271 | + |
| 272 | + private _scrollToIndex(index: number, animated: boolean = true) { |
| 273 | + if (!this._ios) { |
| 274 | + return; |
| 275 | + } |
| 276 | + |
| 277 | + const itemsLength = this.items ? this.items.length : 0; |
| 278 | + // mimic Android behavior that silently coerces index values within [0, itemsLength - 1] range |
| 279 | + if (itemsLength > 0) { |
| 280 | + if (index < 0) { |
| 281 | + index = 0 |
| 282 | + } else if (index >= itemsLength) { |
| 283 | + index = itemsLength - 1; |
| 284 | + } |
| 285 | + |
272 | 286 | this._ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), |
273 | | - UITableViewScrollPosition.Top, true); |
| 287 | + UITableViewScrollPosition.Top, animated); |
| 288 | + } else if (trace.isEnabled()) { |
| 289 | + trace.write(`Cannot scroll listview to index ${index} when listview items not set`, trace.categories.Binding); |
274 | 290 | } |
275 | 291 | } |
276 | 292 |
|
|
0 commit comments