c# - Xamarin-ios 小部件无法加载

标签 c# ios xamarin xamarin.ios tableview

在我的小部件中,我有表格 View 和 5 行单元格。我正在获取数据并尝试初始化为 Uilabel。 我正在尝试编写将数据初始化到单元格中的数据源。 我没有任何构建错误,但它没有调用 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;
            }
        }

    }
}

最佳答案

这是因为您没有为委托(delegate)方法使用Export属性。

当您在Xamarin中使用GetCell时,它找不到iOS中的绑定(bind)方法。

修改如下

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

}

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

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

强大的代表

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)
    {

    }
}

弱代表

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

关于c# - Xamarin-ios 小部件无法加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47840334/

相关文章:

c# - [STAThread] 是做什么的?

c# - GridView排序和分页

ios - 如何在向下滚动时隐藏导航栏和工具栏? Swift(如 myBridge 应用程序)

ios - 如何正确清空通过 UIStoryboard 实例化的 UITableView?

c# - Xamarin Forms - ListView 中的 iOS 动态 ViewCell 大小

xamarin - 如何在 Xamarin 中的 ScrollView 内的 StackLayout 内正确使用 AbsoluteLayout?

c# - 网络办公室 Hello World

c# - 如何从 C# web 服务生成 WSDL 文件

ios - Swift 的 openURL 方法不会调用带有星号的电话 (iOS 8.4)

c# - 在屏幕中缩小时,部分 View 神奇地消失了(Android 上的 Xamarin Forms)