Ich habe jetzt eine Lösung. Kann jemand bitte überprüfen, ob ich es richtig mache, nicht dass ich Programmierungsmuster oder Richtlinien nicht verletze.
Kurz gesagt: In der TableSource-Klasse greife ich auf das Storyboard zu und erzeuge und zeige dynamisch denselben Controller, der auch tatsächlich dargestellt wird. Aber bevor ich das neue vorstelle, erkläre ich eine andere Quellklasse. Damit das TableView zur Anzeige von Adressbüchern und Adresskarten verwendet wird, wird der UIViewController zur Anzeige der Kartendaten verwendet. Diese beiden Controller sind über einen Strang verbunden.
Hier sind mein Controller und beide Quelle-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);
}
.....
}
Es funktioniert. Aber mache ich es richtig? Danke
Was ist mit dem Definieren von Kategorien und Artikeln in der Datenbank, und holen Sie sie während der Laufzeit? – Zbidi
Ich erhalte diese Daten über eine API über WAN/LAN. Aber die Daten zu bekommen, ist nicht das Problem. Ich weiß nicht, wie man den tableviewcontroller mit n subcategory-levels und einer abschließenden Artikelebene programmatisch wiederverwenden kann. Das dachte ich mir im Konzept so: Daten in Tabelle anzeigen. Berühren Sie -> Unterkategorien anzeigen, Ebene 1 mit dieser Tabelle (und Navigationsstapel) wiederverwenden -> berühren -> anzeigen -> berühren -> anzeigen .... zuletzt Artikelliste anzeigen -> berühren -> UIViewController mit Artikeldetails laden. Wie mache ich das über Code? Ist das der richtige Weg? Ich hoffe du verstehst. – MikeRoss