使用環境
PC:MaxOSX
環境:Visual Studio Community 2016 for mac
言語:c#
やりたいこと
- TableViewに手で直接文字列を入力し、その入力した文字列の中から、選択行にある文字列を参照したい。
- あらかじめ保存していたテキストデータなどをTableViewに表示したい。
前提
状況1:TableViewが設置されているのはMainWindowから派生したViewControllerの中にあります。
状況2:表にて入力する過程は、以前に質問させて頂きました「TableViewへ文字列の入力と、行の追加・方法を教えて下さい」で可能となりました。
その節は大変おせわになりました。
本質問は前質問の派生となります。
作って有る部分
- TableViewをカスタマイズするために作った部分
using System; using System.Collections.Generic; using AppKit; namespace FreeEncodeMailer { public class Product { #region Computed Propoperties public string Title { get; set; } = ""; public string Description { get; set; } = ""; #endregion #region Constructors public Product() { } public Product(string title, string description) { this.Title = title; this.Description = description; } #endregion } /// <summary> /// TableDataSource /// </summary> public class ProductTableDataSource : NSTableViewDataSource { #region Public Variables public List<Product> Products = new List<Product>(); #endregion #region Constructors public ProductTableDataSource() { } #endregion #region Override Methods public override nint GetRowCount(NSTableView tableView) { return Products.Count; } #endregion } /// <summary> /// ProductTableDelegate /// </summary> public class ProductTableDelegate : NSTableViewDelegate { #region Constants private const string CellIdentifier = "ProdCell"; #endregion #region Private Variables private ProductTableDataSource DataSource; #endregion #region Constructors public ProductTableDelegate(ProductTableDataSource datasource) { this.DataSource = datasource; } #endregion #region Override Methods public override NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, nint row) { // This pattern allows you reuse existing views when they are no-longer in use. // If the returned view is null, you instance up a new view // If a non-null view is returned, you modify it enough to reflect the new data NSTextField view = (NSTextField)tableView.MakeView(tableColumn.Title, this); if (view == null) { view = new NSTextField(); view.Identifier = tableColumn.Title; view.BackgroundColor = NSColor.Clear; view.Bordered = false; view.Selectable = false; view.Editable = true; view.EditingEnded += (sender, e) => { // Take action based on type switch (view.Identifier) { case "Product": DataSource.Products[(int)view.Tag].Title = view.StringValue; break; case "Details": DataSource.Products[(int)view.Tag].Description = view.StringValue; break; } }; } // Tag view view.Tag = row; // Setup view based on the column selected switch (tableColumn.Title) { case "Product": view.StringValue = DataSource.Products[(int)row].Title; break; case "Details": view.StringValue = DataSource.Products[(int)row].Description; break; } return view; } public override bool ShouldSelectRow(NSTableView tableView, nint row) { return true; } #endregion } }
- 画面がロードされた時
public override void ViewDidLoad() { base.ViewDidLoad(); var DataSource = new ProductTableDataSource(); ↓これを入力しても値がなぜか代入されませんでした・・・ //DataSource.Products.Add(new Product("Xamarin.iOS", "Allows you to develop native iOS Applications in C#")); //DataSource.Products.Add(new Product("Xamarin.Android", "Allows you to develop native Android Applications in C#")); //DataSource.Products.Add(new Product("Xamarin.Mac", "Allows you to develop Mac native Applications in C#")); // Populate the Product Table TableView.DataSource = DataSource; TableView.Delegate = new ProductTableDelegate(DataSource); } public override NSObject RepresentedObject { get { return base.RepresentedObject; } set { base.RepresentedObject = value; // Update the view, if already loaded. } }
- ボタンを押して新規入力行を作成
partial void ClickButton_NewRowCreate(NSObject sender) { TableView.BeginUpdates(); var DataSource = new ProductTableDataSource(); DataSource = (FreeEncodeMailer.ProductTableDataSource)TableView.DataSource; DataSource.Products.Add(new Product("", "")); TableView.ReloadData(); TableView.EndUpdates(); }
試したこと①
あらかじめデータを表に入力しておくために、全質問でご紹介いただいたページを参考に以下のように記述したのですが、表に文字列が表示する事ができませんでした。
public override void ViewDidLoad() { base.ViewDidLoad(); var DataSource = new ProductTableDataSource(); DataSource.Products.Add(new Product("Xamarin.iOS", "Allows you to develop native iOS Applications in C#")); DataSource.Products.Add(new Product("Xamarin.Android", "Allows you to develop native Android Applications in C#")); DataSource.Products.Add(new Product("Xamarin.Mac", "Allows you to develop Mac native Applications in C#")); // Populate the Product Table TableView.DataSource = DataSource; TableView.Delegate = new ProductTableDelegate(DataSource); }
また、上やこちらのサイトにあるようにViewDidLoad()内で呼び出さず、AwakeFromNib()で呼び出すと、DataSourceの部分でスローしてしまいました。
試したこと②
TableViewに手で文字列を入力し、その入力した文字列の中から選択行にある文字列を参照するために
以下のようなコードを書きました。
結果としては、空の文字列が返ってくるだけで、入力されて表示されている文字列は参照できませんでした。
var DataSource = new ProductTableDataSource(); DataSource = (FreeEncodeMailer.ProductTableDataSource)TableView.DataSource; var TitleStr = DataSource.Products[(int)TableView.SelectedRow].Title; // <- TitleStrには空の文字列が入ってしまいます
最後に
どうすれば「やりたい事」が実現できるのかわからず、未だ途方に暮れております。
以前にご回答くださった方のおかげでだいぶ前進したのですが、なにぶん初心者すぎて右も左もわからぬ状態です。
何か足りない情報などございましたら、お声掛けくだされば追記いたします。
何卒皆様のお知恵をお貸しください。
ご回答をお待ちしております。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。