动态浏览n个类别

时间:2017-02-28 12:21:17

标签: c# ios xamarin

我正在尝试创建我的第一个iOS项目。我对C#有很好的经验。我收到类别和文章的数据。但是我不知道,在列出文章之前我会得到多少个子类别。浏览子类别应该通过TableView完成。当用户单击某个类别时,子类别(级别1)应显示在tableView中。然后,在触摸子类别后,应显示当前的子类别(级别2)(依此类推)。最后,当没有其他子类别时,应显示文章列表。触摸一篇文章后,应显示带有文章数据的新ViewController。

我的问题是,如何处理子类别的导航并创建segue或某事。像那样。我知道,我不能使用故事板,因为我不知道子类别的数量,它们也有所不同。

如何实现此动态导航?能给我举个例子?我知道如何将数据填充到tableView。只有动态导航才是问题所在。

1 个答案:

答案 0 :(得分:0)

我现在有了解决方案。有人可以检查一下,如果我正确地做了这件事并不是因为我没有违反编程模式或准则。

简而言之:在TableSource-Class中,我访问Storyboard并动态创建并显示实际呈现的相同Controller。但在提出新的之前,我宣布了另一个源类。因此TableView用于显示地址簿和地址卡,UIViewController用于显示carddata。这两个控制器通过segue连接。

这是我的Controller和两个Source-Classes:

AddressbooksController.cs

public AddressbooksController (IntPtr handle) : base (handle)
    {
        displayModel = "addressbooks";
    }

    private void loadAddressbooks()
    {
        var AppDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
        addressbooks = AppDelegate.api.DeserializeEntities<YAddressbook>(Task.Run(async () => await AppDelegate.api.Get("Yaddressbooks")).Result);
    }

    public void loadAddresscards()
    {
        var AppDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
        addresscards = AppDelegate.api.DeserializeEntities<Ycard>(Task.Run(async () => await AppDelegate.api.Get("Ycards")).Result);
    }

    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        base.PrepareForSegue(segue, sender);

        // do first a control on the Identifier for your segu
        if (segue.Identifier.Equals("segueShowAddress"))
        {

            var viewController = (AddresscardController)segue.DestinationViewController;
            viewController.card = Card2Display;
        }
    }

    public override void ViewDidLoad()
    {
        if (displayModel == "addressbooks")
        {
            loadAddressbooks();
            tableView.Source = new AddressbooksTableSource(addressbooks.data, this);
            this.NavigationController.Title = "Addressbücher";
        }
        else if (displayModel == "addresscards")
        {
            loadAddresscards();
            tableView.Source = new AddresscardsTableSource(addresscards.data, this);
            this.NavigationController.
        }

        base.ViewDidLoad();
    }

AddressbooksTableSource

public class AddressbooksTableSource: UITableViewSource
{

    private List<YAddressbook> addressbooks;
    private string cellIdentifier = "AddressbooksCell";
    private UINavigationController navigationController;


    public AddressbooksTableSource(List<YAddressbook> books, AddressbooksController ab)
    {
        addressbooks = books;
        this.navigationController = ab.ParentViewController as UINavigationController;
        Console.WriteLine(ab.displayModel);
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        Console.WriteLine("Row selected" + addressbooks[indexPath.Row].displayname);
        UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);

        AddressbooksController newab = Storyboard.InstantiateViewController("AddressbooksViewController") as AddressbooksController;
        newab.displayModel = "addresscards";
        navigationController.PushViewController(newab, true);

        tableView.DeselectRow(indexPath, true);
    }


    ....
}

AddresscardsTableSource

public class AddresscardsTableSource: UITableViewSource
{
    private List<Ycard> addresscards;
    UINavigationController navigationController;
    AddressbooksController ab;
    string cellIdentifier = "AddresscardCell";

    public AddresscardsTableSource(List<Ycard> cards, AddressbooksController vc)
    {
        addresscards = cards;
        navigationController = vc.ParentViewController as UINavigationController;
        ab = vc;
        //navigationController = tableview;
    }

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
    {
        Console.WriteLine("Row selected" + addresscards[indexPath.Row].carddata);
        //UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);

        ab.Card2Display = addresscards[indexPath.Row];
        ab.PerformSegue("segueShowAddress", indexPath);

        //AddresscardController ab = Storyboard.InstantiateViewController("AddresscardViewController") as AddressbooksController;
        //ab.TableView.Source = this;
        //navigationController.PushViewController(ab, true);

        tableView.DeselectRow(indexPath, true);
    }

    .....

}

有效。但我正确地做到了吗?感谢