首页 > 解决方案 > Nativescript iOS 委托 listView(延迟加载?)

问题描述

我想在 nativescript 中的列表视图上使用 onScroll 事件,但该事件不存在。所以我通过本机,所以我想为我的列表视图创建一个委托。一切正常,但我不知道为什么,我必须创建一个 console.log 我的委托才能使用它,否则它不会运行。你知道原因吗?

export class ItemsComponent implements OnInit, AfterViewInit {
   items: Array<Item>;

   @ViewChild('header') header: ElementRef;
   @ViewChild('listView') listView: ElementRef;

   // This pattern makes use of Angular’s dependency injection implementation to
   // inject an instance of the ItemService service into this class.
   // Angular knows about this service because it is included in your app’s main NgModule,
   // defined in app.module.ts.
   constructor(private itemService: ItemService, private zone: NgZone) { }

   ngOnInit(): void {
       this.items = this.itemService.getItems();
   }

   ngAfterViewInit() {
       setTimeout(() => {
          if (isIOS) {
               let newDelegate = newUITableViewDelegateImpl.initWithOriginalDelegate(this.listView.nativeElement._delegate);
               this.listView.nativeElement.ios.delegate = newDelegate;
               console.log(this.listView.nativeElement.ios.delegate);
           }
       }, 50)

   }
}


class newUITableViewDelegateImpl extends NSObject implements UITableViewDelegate {
    public static ObjCProtocols = [UITableViewDelegate];

    private _originalDelegate: UITableViewDelegate;

    public static initWithOriginalDelegate(originalDelegate: UITableViewDelegate): newUITableViewDelegateImpl {
        console.log("initWithOwner")

        let delegate = <newUITableViewDelegateImpl>newUITableViewDelegateImpl.new();
        delegate._originalDelegate = originalDelegate;

        return delegate;
    }

    public scrollViewWillBeginDragging(scrollView: UIScrollView) {
        console.log("scrollViewWillBeginDragging");
    }
    public scrollViewDidScroll(scrollView: UIScrollView) {
        console.log("scrollViewDidScroll");

    }
}

标签: iosuitableviewnativescript

解决方案


您必须保存newDelegate某处的 JS 引用,否则它可能会被标记为垃圾回收。

此外,我建议使用RadListView进行延迟加载,因为它具有用于此目的的特定事件。


推荐阅读