gpt4 book ai didi

c# - 无法导航到 RowSelect 上的 ViewController

转载 作者:行者123 更新时间:2023-11-28 23:38:08 25 4
gpt4 key购买 nike

我有 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: Object reference not set to an instance of an object at TravelApp.DataSources.WishlistSource+d__7.MoveNext () [0x000bb] in /Users/evhenijsuhomlyn/Documents/GitHub/TravelAppiOS/TravelApp/DataSources/WishlistSource.cs:53

我检查了 Controller 和 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;
}
}

哪里会出问题?

最佳答案

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

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

关于c# - 无法导航到 RowSelect 上的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54403737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com