2016-12-05 70 views
0

我试图管理XAMrin.iOS中的UIControllerView内的UICollectionView。 我试过很多东西,像这样:https://developer.xamarin.com/guides/ios/tvos/user-interface/collection-views/或这样的:https://www.youtube.com/watch?v=W4H9uLjoEjMXamarin.iOS UICollectionView与自定义UICollectionViewSource崩溃

最后,我跟着这个教程: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) 
    { 
    } 
} 

回答

1

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

我这里忘了 “覆盖” 的文章:

public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) 

应该

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath) 

哎呀

现在,一切都OK了!没有崩溃,并且用户的行为很好触发