Xamarin-ios Widgets无法加载

问题描述 投票:0回答:1

在我的小部件中,我有tableview和5行的单元格。我正在获取数据并尝试初始化为Uilabels。我正在尝试编写将数据初始化为单元格的数据源。我没有任何构建错误,但它没有调用GetCell方法,我打破了它但没有发生任何事情。

在小部件中还有文本“无法加载数据”

这是我的数据源代码

TodayViewController.cs

using System;
using System.Collections.Generic;
using NotificationCenter;
using Foundation;
using UIKit;
using CryptoCurrencyPCL.POCO;
using CryptoCurrencyPCL.Middleware;
using System.Linq;

namespace CryptoTodayWidget
{
    public partial class TodayViewController : UIViewController, INCWidgetProviding,IUITableViewDataSource,IUITableViewDelegate
    {

        const string ReuseId = "currencyCellReuseId";
        List<CoinsPrices> _coins;
        protected TodayViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell(ReuseId, indexPath) as WidgetCell;
            GetData();
            var item = _coins[indexPath.Row];

            cell.InitData(item);

            return cell;
        }

        public nint RowsInSection(UITableView tableView, nint section)
        {
            return _coins?.Count ?? 0;
        }

        [Export("tableView:heightForRowAtIndexPath:")]
        public nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return 50;
        }

        [Export("numberOfSectionsInTableView:")]
        public nint NumberOfSections(UITableView tableView)
        {
            return 1;
        }

        public async void GetData()
        {
            var symbols = await DatabaseManager.Instance.GetRecentCoinsAsync(5);

            var webClient = CryptoCurrencyPCL.Services.CryptoWebClient.Instance;


            List<string> coinSymbols = new List<string>();

            foreach (var item in symbols)
            {
                coinSymbols.Add(item.symbol);
            }


            _coins = await webClient.GetCoinsWithDetailsAsync(coinSymbols);
        }


        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            tableView.DataSource = this;
            tableView.Delegate = this;
            tableView.ReloadData();
            PreferredContentSize = new CoreGraphics.CGSize(320, _coins.Count * 50);
            // Do any additional setup after loading the view.
        }

        [Export("widgetPerformUpdateWithCompletionHandler:")]
        public void WidgetPerformUpdate(Action<NCUpdateResult> completionHandler)
        {
            // Perform any setup necessary in order to update the view.

            // If an error is encoutered, use NCUpdateResultFailed
            // If there's no update required, use NCUpdateResultNoData
            // If there's an update, use NCUpdateResultNewData

            completionHandler(NCUpdateResult.NewData);

        }
    }
}

这是我的Widgetcell.cs

using System;
using CryptoCurrencyPCL.POCO;
using Foundation;
using UIKit;

namespace CryptoTodayWidget
{
    public partial class WidgetCell : UITableViewCell
    {


        public WidgetCell (IntPtr handle) : base (handle)
        {
        }

        public void InitData(CoinsPrices coin){

            coinNameLbl.Text = coin.Coin.Name;
            coinPriceLbl.Text = coin.Detail.PRICE.ToString();
            percentLbl.Text = coin.Detail.CHANGEPCT24HOUR.ToString();

            if (coin.Detail.CHANGEPCT24HOUR < 0)
            {
                percentHolderView.BackgroundColor = Theme.DownColor;
            }

            else if (coin.Detail.CHANGE24HOUR > 0)
            {
                percentHolderView.BackgroundColor = Theme.UpColor;
            }
            else
            {
                percentHolderView.BackgroundColor = Theme.DownColor;
            }
        }

    }
}
c# ios xamarin xamarin.ios tableview
1个回答
1
投票

这是因为你没有使用Export属性作为委托方法。

在Xamarin中使用GetCell时,它无法在iOS中找到绑定方法。

修改如下

[Export("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{

}

但是,我建议您采用正式方式完成这项工作。

在Xamarin.iOS中,我们经常使用Strong DelegatesWeak Delegates来实现iOS中的delegate protocol

Strong Delegates

tableView.DataSource = new MyDelegate();
tableView.Delegate = new MyDataSource();

class MyDelegate: UITableViewDelegate
{
    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        return 10;
    }
}

class MyDataSource: UITableViewDataSource
{
    public override nint RowsInSection(UITableView tableView, nint section)
    {

    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {

    }
}

Weak Delegates

tableView.WeakDelegate= this;
tableView.WeakDataSource= this;

[Export("tableView:heightForRowAtIndexPath:")]
public nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
    return 50;
}

[Export("numberOfSectionsInTableView:")]
public nint NumberOfSections(UITableView tableView)
{
    return 1;
}

[Export("tableView:cellForRowAtIndexPath:")]
public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{

}

请参阅Strong Delegates vs. Weak Delegates

© www.soinside.com 2019 - 2024. All rights reserved.