【问题标题】:passing data to static table - not able to access TableViewCell cells value将数据传递给静态表 - 无法访问 TableViewCell 单元格值
【发布时间】:2017-05-01 01:35:35
【问题描述】:

我正在尝试构建 iOS 应用程序,其中一个视图控制器正在调用(推送)另一个视图控制器。在第二个视图控制器中,我有静态 TableViewController 与 1 个部分和 2 个静态单元格。

每当我访问单元格的元素时,代码都会引发异常。

View Controller 2 通过以下代码推送:

var destinationController = new EditAccountTableViewController(accounts[indexPath.Row]);
parentController.NavigationController.PushViewController(destinationController, true);`

EditAccountTablewViewController 类:

public partial class EditAccountTableViewController : UITableViewController
{
    private Account account;

    public EditAccountTableViewController()
    {
        var loc = NSBundle.MainBundle.LocalizedString("New", "");
        var loc2 = NSBundle.MainBundle.LocalizedString("Account", "");
        Title = $"{loc} {loc2}";

        this.account = new Account();
        AccountNameCell.DetailTextLabel.Text = account.Name;
        AccountDescriptionCell.DetailTextLabel.Text = account.Description;
    }

    public EditAccountTableViewController(Account account)
    {
        var loc = NSBundle.MainBundle.LocalizedString("Account", "");
        Title = $"{loc}";

        this.account = account;
        AccountNameCell.DetailTextLabel.Text = account.Name;
        AccountDescriptionCell.DetailTextLabel.Text = account.Description;
    }

    partial void SaveAccountButton_Activated(UIBarButtonItem sender)
    {
        if (account.Name != string.Empty)
        {
            Database.Instance.DataAccess.SaveModelItem<Account>(account);
        }
    }
}

AccountNameCellAccountDescriptionCell 是单元格名称。单元格在此处的情节提要中创建:

每当我运行代码时,都会出现以下异常:

我很清楚,该异常是由AccountNameCell 引起的,即null。我不明白为什么是null,也不明白如何解决。

感谢您的帮助。

【问题讨论】:

    标签: xcode uitableview xamarin uiviewcontroller tableview


    【解决方案1】:

    我使用 segue 而不是推送 viewController 解决了这个问题,我在其中利用了 prepareForSegue 方法。这是我正在使用的代码:

    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        base.PrepareForSegue(segue, sender);
    
        if (segue.Identifier == "EditAccountSegue")
        {
            var secondViewController = segue.DestinationViewController as EditAccountTableViewController;
            var selectedPath = TableView.IndexPathForSelectedRow;
            var accountSource = TableView.Source as AccountsSettingSource;
            var account = accountSource.getItem(selectedPath.Row);
            secondViewController.Account = account;
        }
        else if (segue.Identifier == "AddAccountSegue")
        {
            var destinationController = segue.DestinationViewController as EditAccountTableViewController;
            destinationController.Account = new Account();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2017-08-18
      相关资源
      最近更新 更多