无法导航到RowSelect上的ViewController

时间:2019-01-28 14:08:45

标签: c# ios xamarin xamarin.ios

我有UITableView

这是代码

public partial class WishlistTableController : UITableViewController
{
    public WishlistTableController (IntPtr handle) : base (handle)
    {
    }
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        List<WishlistDatabaseModel> WishlistTableItems = ExperienceMethods.GetWishlistItems();
        WishlistTableView.Source = new WishlistSource(WishlistTableItems, this, this.NavigationController);
        WishlistTableView.RowHeight = UITableView.AutomaticDimension;
        WishlistTableView.EstimatedRowHeight = 80f;
        WishlistTableView.ReloadData();
    }
}

我还有这个TableView的数据源,我需要在RowSelect上转到另一个ViewController。

但是当我单击行时,出现此错误

  

System.NullReferenceException:对象引用未设置为对象的实例     在/Users/evhenijsuhomlyn/Documents/GitHub/TravelAppiOS/TravelApp/DataSources/WishlistSource.cs:53

中的TravelApp.DataSources.WishlistSource + d__7.MoveNext()[0x000bb]

我检查了控制器和Storuboard ID,一切正常。

这是代码

public class WishlistSource : UITableViewSource
{
    private UINavigationController primNav { get; }
    readonly List<WishlistDatabaseModel> _tableItems;
    private WishlistTableController owner;

    public WishlistSource(List<WishlistDatabaseModel> items, WishlistTableController owner,
        UINavigationController nav)
    {
        _tableItems = items;
        this.owner = owner;
        primNav = nav;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {

        if (_tableItems.Count == 0)
        {
            var noDataLabel = new UILabel
            {
                Text = "No Experiences at your location at this time. Try to change destination",
                TextColor = UIColor.Black,
                TextAlignment = UITextAlignment.Center,
                LineBreakMode = UILineBreakMode.WordWrap,
                Lines = 0
            };
            tableview.BackgroundView = noDataLabel;
            tableview.SeparatorStyle = UITableViewCellSeparatorStyle.None;
            return _tableItems.Count;
        }

        tableview.BackgroundView = null;
        tableview.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine;
        return _tableItems.Count;
    }
    public override async void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        var selectedExperience = await ExperienceMethods.GetSelectedTour(_tableItems[indexPath.Row].id);
        if (selectedExperience == "Saved")
        {
            MyExperienceDetailViewController MyExperienceDetailController = primNav.Storyboard.InstantiateViewController("MyExperienceDetailViewController") as MyExperienceDetailViewController;
            primNav.PushViewController(MyExperienceDetailController, true);
        }
        tableView.DeselectRow(indexPath, true);
    }
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell("wishlistcell", indexPath) as WishlistCell;
        WishlistDatabaseModel item = _tableItems[indexPath.Row];
        cell.UpdateCell(item);
        return cell;
    }
}

哪里有问题?

1 个答案:

答案 0 :(得分:1)

您应该尝试使用另一种方法来创建故事板。

var storyboard = UIStoryboard.FromName("YourStoryboard", null); 
var viewController = storyboard.InstantiateViewController(nameof(YourViewController)); 
NavigationController.PushViewController(viewController, true);