【问题标题】:Xamarin.iOS UICollectionView with custom UICollectionViewSource crashesXamarin.iOS UICollectionView 与自定义 UICollectionViewSource 崩溃
【发布时间】:2016-12-05 13:29:54
【问题描述】:

我尝试在 Xamarin.iOS 的 UIControllerView 内管理 UICollectionView。 我尝试了很多东西,比如:https://developer.xamarin.com/guides/ios/tvos/user-interface/collection-views/ 或这个:https://www.youtube.com/watch?v=W4H9uLjoEjM

最后,我遵循了这个教程:https://forums.xamarin.com/discussion/11274/how-to-setup-uicollectionview-datasource

但我仍然被阻止。我的应用程序在尝试显示带有此错误的 UIViewController 时崩溃:[Gotago_iOS_CollectionViewSource_ViewSourceListProgram collectionView:cellForItemAtIndexPath:]: unrecognized selector sent to instance 0x7af49db0'

这是我的代码...有什么帮助吗?

public partial class ViewControllerListProgram : UIViewController
{
    private ViewSourceListProgram m_viewSource; 

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

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

            AppDelegate.CurrentViewController = this;

            ApiController_GetListConseilCircuit apiCtrl = new ApiController_GetListConseilCircuit();

            m_collectionView.RegisterClassForCell(typeof(CellListProgram), CellListProgram.Id);
            m_viewSource = new ViewSourceListProgram(await apiCtrl.OnlineGetListConseilCircuit());
            m_collectionView.Source = m_viewSource;
        }
    }



public class ViewSourceListProgram : UICollectionViewSource
    {
        public List<ApiController_GetListConseilCircuit.ConseilCircuit> m_lsCircuits;

    public ViewSourceListProgram(List<ApiController_GetListConseilCircuit.ConseilCircuit> lsCircuits)
    {
        m_lsCircuits = lsCircuits;
    }

    public override nint NumberOfSections(UICollectionView collectionView)
    {
        return 1;
    }

    public override nint GetItemsCount(UICollectionView collectionView, nint section)
    {
        return m_lsCircuits.Count;
    }

    public override bool ShouldHighlightItem(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
    {
        return true;
    }

    public override void ItemHighlighted(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
    {
        CellListProgram cell = (CellListProgram)collectionView.CellForItem(indexPath);

        Toast.MakeText("Cell highlighted", Toast.LENGTH_LONG).Show();
    }

    public override void ItemUnhighlighted(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
    {
        CellListProgram cell = (CellListProgram)collectionView.CellForItem(indexPath);

        Toast.MakeText("Cell unhighlighted", Toast.LENGTH_LONG).Show();
    }

    public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        CellListProgram cell = (CellListProgram)collectionView.DequeueReusableCell(CellListProgram.Id, indexPath);

        ApiController_GetListConseilCircuit.ConseilCircuit circuitOrProgram = m_lsCircuits[indexPath.Row];

        return cell;
    }
}


public partial class CellListProgram : UICollectionViewCell
    {
        public static readonly NSString Id = new NSString("programCell");
    public CellListProgram (IntPtr handle) : base (handle)
    {
    }
}

【问题讨论】:

    标签: ios xamarin xamarin.ios uicollectionview


    【解决方案1】:

    好的...经过几个小时的可怕搜索后,我发现了我的错误。

    我在这里忘记了“覆盖”关键字:

    public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    

    应该是

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    

    现在,一切正常!没有崩溃,用户的动作被很好地触发了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多