ios - UITableViewSource 从不创建单元格

标签 ios xamarin.ios mvvmcross

我正在使用 MvvmCross 编写 Xamarin.iOS 应用程序。我正在尝试制作一个表格,我可以看到绑定(bind)到源中的项目,但我从未看到任何单元格被创建。函数GetOrCreateCellFor永远不会被调用。这是我的代码:

public class ContactsManager
{
    ContactsView _contactsView;

    public ContactsManager()
    {
        _contactsView = new ContactsView();
        Source = new FriendTableViewSource(_contactsView.FriendsTableView);
        _contactsView.FriendsTableView.Source = Source;
    }

    public FriendTableViewSource Source { get; set; }
}

public class FriendTableViewSource : MvxTableViewSource
{
    private readonly List<SeeMyFriendsItemViewModel> _content = new List<SeeMyFriendsItemViewModel>();
    private readonly UITableView _tableView;

    public FriendTableViewSource(UITableView t) : base(t)
    {
        _tableView = t;
        t.RegisterNibForCellReuse(UINib.FromName(FriendCell.Key, NSBundle.MainBundle), FriendCell.Key);
    }

    private void Init(IEnumerable<SeeMyFriendsItemViewModel> items)
    {
        _content.Clear();
        _content.AddRange(items);
    }

    public override System.Collections.IEnumerable ItemsSource
    {
        get
        {
            return base.ItemsSource;
        }
        set
        {
            // I put a break point here to check if I'm getting the items, and it is, so the binding is fine...
            if (value != null)
                Init(value.Cast<SeeMyFriendsItemViewModel>());
            base.ItemsSource = value;

            _tableView.ReloadData();
        }
    }

    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        return 60;
    }

    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        // This function never gets called!
        return TableView.DequeueReusableCell(FriendCell.Key, indexPath);
    }
}

[Register("FriendCell")]
public class FriendCell : MvxTableViewCell
{
    public static readonly NSString Key = new NSString("FriendCell");
    public static readonly UINib Nib;

    static FriendCell()
    {
        Nib = UINib.FromName("FriendCell", NSBundle.MainBundle);
    }

    protected FriendCell(IntPtr handle) : base(handle)
    {
        BackgroundColor = UIColor.Red;
    }
}

编辑

这就是源代码的工作版本应该是什么样的。同样有趣的是,如果表格未添加到您的 View 中,则不会调用 GetOrCreateCellFor

public class FriendTableViewSource : MvxTableViewSource
{
    private readonly List<SeeMyFriendsItemViewModel> _content = new List<SeeMyFriendsItemViewModel>();
    private MvxNotifyCollectionChangedEventSubscription _subscription;

    public FriendTableViewSource(UITableView t) : base(t)
    {
        t.RegisterClassForCellReuse(typeof(FriendCell), FriendCell.Key);
    }

    private void Init(IEnumerable<SeeMyFriendsItemViewModel> items)
    {
        _content.Clear();
        _content.AddRange(items);
    }

    public override System.Collections.IEnumerable ItemsSource
    {
        get
        {
            return base.ItemsSource;
        }
        set
        {
            if (value != null)
            {
                Init(value.Cast<SeeMyFriendsItemViewModel>());

                var collectionChanged = value as System.Collections.Specialized.INotifyCollectionChanged;
                if (collectionChanged != null)
                {
                    _subscription = collectionChanged.WeakSubscribe(CollectionChangedOnCollectionChanged);
                }
            }
            base.ItemsSource = value;

            ReloadTableData();
        }
    }

    protected override void CollectionChangedOnCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
    {
        if (args.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            foreach (var item in args.NewItems)
            {
                var chatItem = item as SeeMyFriendsItemViewModel;
                _content.Add(chatItem);
            }
        }

        Init(ItemsSource.Cast<SeeMyFriendsItemViewModel>());
        base.CollectionChangedOnCollectionChanged(sender, args);

        InvokeOnMainThread(() => {
            ReloadTableData();

            TableView.SetContentOffset(new CGPoint(0, TableView.ContentSize.Height - TableView.Frame.Size.Height), true);
        });
    }

    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        return 60;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return _content.Count();
    }

    public override nint NumberOfSections(UITableView tableView)
    {
        return 1;
    }

    protected override object GetItemAt(NSIndexPath indexPath)
    {
        if (indexPath.Row < _content.Count)
            return _content[indexPath.Row];

        return null;
    }

    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        return TableView.DequeueReusableCell(FriendCell.Key, indexPath);
    }
}

最佳答案

覆盖 FriendTableViewSource 中的 RowsInSection

由于tableview需要行数和行高来决定其框架,因此如果height = 0或count = 0,则永远不会调用GetOrCreateCellFor

关于ios - UITableViewSource 从不创建单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46229111/

相关文章:

iphone - 以编程方式检测已安装的应用程序

ios - 带有 Firebase 的 Microsoft 推荐 API

ios - swift 3 : Unrecognized selector sent to instance Xcode 8

wpf - MVVMCross,WPF,XAML “Unable to resolve the binding creator - have you initialized Windows Binding”

c# - 无效的 il 代码 System.IO.Compression.ZipFile.OpenRead() 方法主体为空

ios - 具有不同圆角半径的 WatchKit WKInterfaceGroup

ipad - IOS:UIDatePicker 呈现黑色背景效果不佳

c# - 使用 Xamarin 的 iOS 应用的安装日期

ios - 如何调试 Apple App Loader 中的 "The app references non-public selectors"错误?

c# - 无法将 Xamarin.Android 和 Xamarin.iOS 添加到 PCL