2017-02-28 55 views
0

我正在嘗試創建我的第一個iOS項目。我有很好的C#經驗。我收到有關類別和文章的數據。但我不知道,在文章上市之前,我會得到多少個子類別。瀏覽子類別應該通過TableView完成。當用戶點擊某個類別時,子類別(級別1)應顯示在tableView中。然後,在觸摸子類別之後,應該顯示當前的子類別(等級2)(依此類推)。最後,當沒有其他子類別時,應顯示文章列表。觸摸一篇文章後,應顯示帶有文章數據的新ViewController。動態導航n個類別

我的問題是,如何處理通過子類別的導航和創建segues或某事。像那樣。我知道,我不能爲此使用故事板,因爲我不知道子類別的數量,它們也有所不同。

我該如何實現這種動態導航?你可以給我一個例子嗎?我知道如何將數據填充到tableView。只有動態導航是問題。

+0

如何定義數據庫中的類別和文章,並在運行時獲取它們? – Zbidi

+0

我通過廣域網/局域網上的API接收這些數據。但獲取數據不是那個問題。我不知道如何以編程方式重用具有n個子類別級別和最終文章級別的tableviewcontroller。我在概念中想到了這樣的東西:在表格中顯示數據。觸摸 - >顯示子級與重用此表(和導航堆棧) - >觸摸 - >顯示 - >觸摸 - >顯示....最後顯示文章列表 - >觸摸 - >加載UIViewController與文章詳細信息。我如何通過代碼來做到這一點。這是正確的方式嗎?希望你能理解。 – MikeRoss

回答

0

我現在有一個解決方案。有人可以檢查,如果我正確地做,不是我不違反編程模式或指南。

簡而言之:在TableSource-Class中,我訪問Storyboard並動態創建並顯示實際呈現的相同控制器。但是在提交新的之前,我宣佈了另一個源類。因此,TableView用於顯示通訊錄和地址卡,UIViewController用於顯示卡片數據。這兩個控制器通過segue連接。

這裏是我的控制器和兩個來源 - 類:

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); 
    } 

    ..... 

} 

它的工作原理。但我做得對嗎?謝謝

+0

有人嗎?它是否正確? – MikeRoss