Skip to content

Commit 52c0448

Browse files
authored
feat(ios-list-view): introduce iosEstimatedRowHeight property. (NativeScript#5568)
* feat(ios-list-view): introduce iosEstimatedRowHeight property. * chore(ui-tests-app): add test for auto measured ListView.
1 parent 0012bfd commit 52c0448

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

apps/app/ui-tests-app/list-view/scrolling-and-sizing.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,15 @@
5151
</ListView>
5252
</StackLayout>
5353

54+
<StackLayout class="p-10" row="3">
55+
<Label text="ios-estimated-row-height" class="body m-b-10" />
56+
<ListView items="{{ $value }}" iosEstimatedRowHeight="0">
57+
<ListView.itemTemplate>
58+
<Label text="{{ $value }}" />
59+
</ListView.itemTemplate>
60+
</ListView>
61+
<Label text="after-auto-estimated-height" class="body m-b-10" />
62+
</StackLayout>
63+
5464
</StackLayout>
5565
</Page>

tns-core-modules/ui/list-view/list-view-common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export abstract class ListViewBase extends View implements ListViewDefinition, T
4242
public _itemTemplatesInternal = new Array<KeyedTemplate>(this._defaultTemplate);
4343
public _effectiveRowHeight: number = autoEffectiveRowHeight;
4444
public rowHeight: Length;
45+
public iosEstimatedRowHeight: Length;
4546
public items: any[] | ItemsSource;
4647
public itemTemplate: string | Template;
4748
public itemTemplates: string | Array<KeyedTemplate>;
@@ -210,5 +211,10 @@ export const rowHeightProperty = new CoercibleProperty<ListViewBase, Length>({
210211
});
211212
rowHeightProperty.register(ListViewBase);
212213

214+
export const iosEstimatedRowHeightProperty = new Property<ListViewBase, Length>({
215+
name: "iosEstimatedRowHeight", valueConverter: (v) => Length.parse(v)
216+
});
217+
iosEstimatedRowHeightProperty.register(ListViewBase);
218+
213219
export const separatorColorProperty = new CssProperty<Style, Color>({ name: "separatorColor", cssName: "separator-color", equalityComparer: Color.equals, valueConverter: (v) => new Color(v) });
214220
separatorColorProperty.register(Style);

tns-core-modules/ui/list-view/list-view.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ export class ListView extends View {
7878
*/
7979
rowHeight: Length;
8080

81+
/**
82+
* Gets or set the estimated height of rows in the ListView.
83+
* The default value is 44px.
84+
*/
85+
iosEstimatedRowHeight: Length;
86+
8187
/**
8288
* Forces the ListView to reload all its items.
8389
*/
@@ -188,6 +194,11 @@ export const separatorColor: Property<ListView, Color>;
188194
*/
189195
export const rowHeightProperty: Property<ListView, Length>;
190196

197+
/**
198+
* Represents the observable property backing the iosEstimatedRowHeight property of each ListView instance.
199+
*/
200+
export const iosEstimatedRowHeightProperty: Property<ListView, Length>;
201+
191202
/**
192203
* Backing property for separator color property.
193204
*/

tns-core-modules/ui/list-view/list-view.ios.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ItemEventData } from ".";
33
import {
44
ListViewBase, View, KeyedTemplate, Length, Observable, Color,
5-
separatorColorProperty, itemTemplatesProperty, layout, EventData
5+
separatorColorProperty, itemTemplatesProperty, iosEstimatedRowHeightProperty, layout, EventData
66
} from "./list-view-common";
77
import { StackLayout } from "../layouts/stack-layout";
88
import { ProxyViewContainer } from "../proxy-view-container";
@@ -138,7 +138,7 @@ class UITableViewDelegateImpl extends NSObject implements UITableViewDelegate {
138138
public tableViewHeightForRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): number {
139139
const owner = this._owner.get();
140140
if (!owner) {
141-
return DEFAULT_HEIGHT;
141+
return tableView.estimatedRowHeight;
142142
}
143143

144144
let height: number;
@@ -188,7 +188,7 @@ class UITableViewRowHeightDelegateImpl extends NSObject implements UITableViewDe
188188
}
189189
return indexPath;
190190
}
191-
191+
192192
public tableViewDidSelectRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): NSIndexPath {
193193
tableView.deselectRowAtIndexPathAnimated(indexPath, true);
194194

@@ -198,7 +198,7 @@ class UITableViewRowHeightDelegateImpl extends NSObject implements UITableViewDe
198198
public tableViewHeightForRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): number {
199199
let owner = this._owner.get();
200200
if (!owner) {
201-
return DEFAULT_HEIGHT;
201+
return tableView.estimatedRowHeight;
202202
}
203203

204204
return owner._effectiveRowHeight;
@@ -345,7 +345,7 @@ export class ListView extends ListViewBase {
345345
return height;
346346
}
347347

348-
return DEFAULT_HEIGHT;
348+
return this._ios.estimatedRowHeight;
349349
}
350350

351351
public _prepareCell(cell: ListViewCell, indexPath: NSIndexPath): number {
@@ -428,4 +428,13 @@ export class ListView extends ListViewBase {
428428

429429
this.refresh();
430430
}
431+
432+
[iosEstimatedRowHeightProperty.getDefault](): Length {
433+
return DEFAULT_HEIGHT;
434+
}
435+
[iosEstimatedRowHeightProperty.setNative](value: Length) {
436+
const nativeView = this._ios;
437+
const estimatedHeight = Length.toDevicePixels(value, 0);
438+
nativeView.estimatedRowHeight = estimatedHeight < 0 ? DEFAULT_HEIGHT : estimatedHeight;
439+
}
431440
}

0 commit comments

Comments
 (0)