Skip to content

Commit 392266b

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#221 from NativeScript/ListView-cell
view parent exposed in itemLoading event
2 parents 433ef14 + d0936df commit 392266b

4 files changed

Lines changed: 50 additions & 15 deletions

File tree

apps/tests/ui/list-view/list-view-tests.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import helper = require("../helper");
44
import viewModule = require("ui/core/view");
55
import observable = require("data/observable");
66
import types = require("utils/types");
7+
import platform = require("platform");
78

89
// <snippet module="ui/list-view" title="list-view">
910
// # ListView
@@ -128,6 +129,34 @@ export function test_set_items_to_array_loads_all_items() {
128129
helper.buildUIAndRunTest(listView, testAction);
129130
}
130131

132+
export function test_set_native_item_exposed() {
133+
var listView = new listViewModule.ListView();
134+
135+
function testAction(views: Array<viewModule.View>) {
136+
var indexes = {};
137+
var colors = ["red", "green", "blue"];
138+
listView.items = colors;
139+
listView.on(listViewModule.ListView.itemLoadingEvent, function (args: listViewModule.ItemEventData) {
140+
if (platform.device.os === platform.platformNames.ios) {
141+
indexes[args.index] = args.ios;
142+
} else if (platform.device.os === platform.platformNames.android) {
143+
indexes[args.index] = args.android;
144+
}
145+
});
146+
147+
TKUnit.wait(ASYNC);
148+
for (var item in indexes) {
149+
if (platform.device.os === platform.platformNames.ios) {
150+
TKUnit.assert(indexes[item] instanceof UITableViewCell, "itemLoading not called for index " + item);
151+
} else if (platform.device.os === platform.platformNames.android) {
152+
TKUnit.assert(indexes[item] instanceof android.view.ViewGroup, "itemLoading not called for index " + item);
153+
}
154+
}
155+
};
156+
157+
helper.buildUIAndRunTest(listView, testAction);
158+
}
159+
131160
export function test_set_items_to_array_creates_native_views() {
132161
var listView = new listViewModule.ListView();
133162
listView.on(listViewModule.ListView.itemLoadingEvent, loadViewWithItemNumber);

ui/list-view/list-view.android.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import stackLayout = require("ui/layouts/stack-layout");
66
import proxy = require("ui/core/proxy");
77
import dependencyObservable = require("ui/core/dependency-observable");
88
import color = require("color");
9+
import definition = require("ui/list-view");
910

1011
var ITEMLOADING = common.ListView.itemLoadingEvent;
1112
var LOADMOREITEMS = common.ListView.loadMoreItemsEvent;
@@ -181,13 +182,17 @@ class ListViewAdapter extends android.widget.BaseAdapter {
181182
return true;
182183
}
183184

184-
public getView(index: number, convertView: android.view.View, parent: any): android.view.View {
185+
public getView(index: number, convertView: android.view.View, parent: android.view.ViewGroup): android.view.View {
185186
if (!this._listView) {
186187
return null;
187188
}
188189

189190
var view = this._listView._getRealizedView(convertView, index);
190-
var args = { eventName: ITEMLOADING, object: this._listView, index: index, view: view };
191+
var args = <definition.ItemEventData>{
192+
eventName: ITEMLOADING, object: this._listView, index: index, view: view,
193+
android: parent,
194+
ios: undefined
195+
};
191196
this._listView.notify(args);
192197

193198
if (!args.view) {

ui/list-view/list-view.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,15 @@ declare module "ui/list-view" {
125125
* The view that is associated to the item, for which the event is raised.
126126
*/
127127
view: view.View;
128+
129+
/**
130+
* Gets the native [iOS view](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/) that represents the user interface where the view is hosted. Valid only when running on iOS.
131+
*/
132+
ios: UITableViewCell;
133+
134+
/**
135+
* Gets the native [android widget](http://developer.android.com/reference/android/view/ViewGroup.html) that represents the user interface where the view is hosted. Valid only when running on Android OS.
136+
*/
137+
android: android.view.ViewGroup;
128138
}
129139
}

ui/list-view/list-view.ios.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,8 @@ require("utils/module-merge").merge(common, exports);
1919

2020
var infinity = utils.layout.makeMeasureSpec(0, utils.layout.UNSPECIFIED);
2121

22-
class ListViewCell extends UITableViewCell {
23-
static new(): ListViewCell {
24-
return <ListViewCell>super.new();
25-
}
26-
static class(): any {
27-
return ListViewCell;
28-
}
29-
}
30-
3122
function notifyForItemAtIndex(listView: definition.ListView, cell: any, eventName: string, indexPath: NSIndexPath) {
32-
var args = <definition.ItemEventData>{ eventName: eventName, object: listView, index: indexPath.row, view: cell.view };
23+
var args = <definition.ItemEventData>{ eventName: eventName, object: listView, index: indexPath.row, view: cell.view, ios: cell, android: undefined };
3324
listView.notify(args);
3425
return args;
3526
}
@@ -54,7 +45,7 @@ class DataSource extends NSObject implements UITableViewDataSource {
5445

5546
public tableViewCellForRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath): UITableViewCell {
5647
// We call this method because ...ForIndexPath calls tableViewHeightForRowAtIndexPath immediately (before we can prepare and measure it).
57-
var cell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || ListViewCell.new();
48+
var cell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || UITableViewCell.new();
5849
this._owner._prepareCell(cell, indexPath);
5950

6051
var cellView: view.View = cell.view;
@@ -115,7 +106,7 @@ class UITableViewDelegateImpl extends NSObject implements UITableViewDelegate {
115106
// in iOS 7.1 this method is called before tableViewCellForRowAtIndexPath so we need fake cell to measure its content.
116107
var cell = this._measureCell;
117108
if (!cell) {
118-
this._measureCell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || ListViewCell.new();
109+
this._measureCell = tableView.dequeueReusableCellWithIdentifier(CELLIDENTIFIER) || UITableViewCell.new();
119110
cell = this._measureCell;
120111
}
121112

@@ -157,7 +148,7 @@ export class ListView extends common.ListView {
157148
super();
158149

159150
this._ios = new UITableView();
160-
this._ios.registerClassForCellReuseIdentifier(ListViewCell.class(), CELLIDENTIFIER);
151+
this._ios.registerClassForCellReuseIdentifier(UITableViewCell.class(), CELLIDENTIFIER);
161152
this._ios.autoresizesSubviews = false;
162153
this._ios.autoresizingMask = UIViewAutoresizing.UIViewAutoresizingNone;
163154

0 commit comments

Comments
 (0)