fix(listview): correct item template resolution in sectioned ListView by VeinDevTtv · Pull Request #11184 · NativeScript/NativeScript · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions apps/automated/src/ui/list-view/list-view-tests.ts
8 changes: 4 additions & 4 deletions packages/core/ui/list-view/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,8 +996,8 @@ function ensureListViewAdapterClass() {
// Header view type is the last index (after all item template types)
return this.owner._itemTemplatesInternal.length;
} else {
// Get template for the actual item
const template = this.owner._getItemTemplate(positionInfo.itemIndex);
// Get template for the actual item using section-aware lookup
const template = this.owner._getItemTemplateInSection(positionInfo.section, positionInfo.itemIndex);
return this.owner._itemTemplatesInternal.indexOf(template);
}
} else {
Expand Down Expand Up @@ -1124,8 +1124,8 @@ function ensureListViewAdapterClass() {
}

private _createItemView(section: number, itemIndex: number, convertView: android.view.View, parent: android.view.ViewGroup): android.view.View {
// Use existing item creation logic but with sectioned data
const template = this.owner._getItemTemplate(itemIndex);
// Use section-aware template lookup when in sectioned mode, flat lookup otherwise
const template = section >= 0 && this.owner.sectioned ? this.owner._getItemTemplateInSection(section, itemIndex) : this.owner._getItemTemplate(itemIndex);
let view: View;

// convertView is of the wrong type
Expand Down
12 changes: 3 additions & 9 deletions packages/core/ui/list-view/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class DataSource extends NSObject implements UITableViewDataSource {
const owner = this._owner?.deref();
let cell: ListViewCell;
if (owner) {
const template = owner._getItemTemplate(indexPath.row);
const template = owner.sectioned ? owner._getItemTemplateInSection(indexPath.section, indexPath.row) : owner._getItemTemplate(indexPath.row);
cell = <ListViewCell>(tableView.dequeueReusableCellWithIdentifier(template.key) || ListViewCell.initWithEmptyBackground());
owner._prepareCell(cell, indexPath);

Expand Down Expand Up @@ -235,7 +235,7 @@ class UITableViewDelegateImpl extends NSObject implements UITableViewDelegate {
let height = owner.getHeight(indexPath.row);
if (height === undefined) {
// in iOS8+ after call to scrollToRowAtIndexPath:atScrollPosition:animated: this method is called before tableViewCellForRowAtIndexPath so we need fake cell to measure its content.
const template = owner._getItemTemplate(indexPath.row);
const template = owner.sectioned ? owner._getItemTemplateInSection(indexPath.section, indexPath.row) : owner._getItemTemplate(indexPath.row);
let cell = this._measureCellMap.get(template.key);
if (!cell) {
cell = <any>tableView.dequeueReusableCellWithIdentifier(template.key) || ListViewCell.initWithEmptyBackground();
Expand Down Expand Up @@ -794,13 +794,7 @@ export class ListView extends ListViewBase {
let view: ItemView = cell.view;
if (!view) {
if (this.sectioned) {
// For sectioned data, we need to calculate the absolute index for template selection
let absoluteIndex = 0;
for (let i = 0; i < indexPath.section; i++) {
absoluteIndex += this._getItemsInSection(i).length;
}
absoluteIndex += indexPath.row;
view = this._getItemTemplate(absoluteIndex).createView();
view = this._getItemTemplateInSection(indexPath.section, indexPath.row).createView();
} else {
view = this._getItemTemplate(indexPath.row).createView();
}
Expand Down
18 changes: 18 additions & 0 deletions packages/core/ui/list-view/list-view-common.ts
Loading