使用環境
PC:MaxOSX
環境:Visual Studio Community 2016 for mac
言語:c#
やりたい事
- TableViewを用いて、以下のことをしたい
- 写真の矢印の部分に文字列を直接入力したい
- 行を追加できるようにした(もしくは初めから大数の行を作成しておきたい)
- 入力されている文字列を参照したい
現状
矢印の部分はStoryboardでNSScrollViewでアウトレットしています
また、以下のコードで1枚目の写真のようにTableのヘッダー部分にタイトルを入れることはできました。
c#
1title_column.Title = "名前"; 2address_column.Title = "アドレス";
試したこと
失敗1
c#
1title_column.TableView.AddColumn(new NSTableColumn("123"));
失敗2
c#
1NSCell cell = new NSCell(); 2cell.StringValue = "123"; 3title_column.DataCell = cell; 4 5cell.StringValue = "345"; 6address_column.DataCell = cell;
最後に
TableViewを用いずに、似たような事ができるコンポーネントがあり、それで解決出来るのでも結構です。
何か表を用いて、そこに入力(手入力、またはどこかから参照入力)が可能で、
入力した値が参照をおこないたいと考えています。
何か説明不足や、疑問点があれば仰ってください。すぐに追記いたします。
何卒、お知恵の拝借をよろしくお願い致します。
追記(1)
ご回答いただきましたURLを元に、少しだけ改良致しました。
そのコードと、結果になります。
private void aaa() { title_column.Title = "名前"; address_column.Title = "アドレス"; tableview.DataSource = new TableDataSource(); tableview.Delegate = new TableDelegate(); TextGrid.DocumentView = tableview; }
- なぜか矢印部分に「Zero」〜「Nine」までの文字か勝手に入力される
- 選択箇所の後ろ側?だけ、青く色が変わっている
- 文字の編集などはできない
今後、回答で頂いたURLを読みながら少しずつ改変していきたいと思います。
何卒よろしくお願いいたします。
追記(2)
ご回答いただきましたソースを元に、以下を作成いたしました。
- 表にキーボードから記入できるようになった
- 表に行を追加することができるようになった
お陰様で、上記二つの問題は解決いたしました。
残る問題は
- 表に記入されているデータを参照する
- あらかじめ表にデータを入力しておく(表に入力したデータをテキストファイルか何かに保存し、それを次回に改めて表示することができる)
の二点となりました。
何卒、解決への糸口をご教授いただければと思います。
現状
解決したソースを掲載させて頂きます。
1.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 } }
2.動作を記述する部分への表記
// 画面がロードされた時 public override void ViewDidLoad() { base.ViewDidLoad(); var DataSource = new ProductTableDataSource(); // 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_NewAddress(NSObject sender) { TableView.BeginUpdates(); var DataSource = new ProductTableDataSource(); DataSource = (FreeEncodeMailer.ProductTableDataSource)TableView.DataSource; DataSource.Products.Add(new Product("", "")); TableView.ReloadData(); TableView.EndUpdates(); }
やりたいこと
- 表に記入されているデータを参照する
- あらかじめ表にデータを入力しておくことができる
- ↑(表に入力したデータをテキストファイルか何かに保存し、それを次回に改めて表示することができる)
上記画像のように、選択されている行の値を参照したいと考えています。
また、上記のように表へあらかじめデータを入力しておき、その続きから改めて行の追加を行いたいと考えています。
まず、
2列目(キーがDetailである列(上記、「現状」のコード参照))を取得しようと、以下のようにコードを書きましたが、結果的に空のStringしか取得できませんでした。
partial void ClickButton_GetValue(NSObject sender) { var DataSource = (FreeEncodeMailer.ProductTableDataSource)TableView.DataSource; string address = DataSource.Products[(int)TableView.SelectedRow].Title; }
→他にもいくつか試したのですが。どれもエラーを吐いてしまい、動作しませんでしたので掲載を省きました。
どうすれば、表の値を参照できるのでしょうか?
また、どうすれば「やりたいこと」を実現できるでしょうか?
度々申し訳ございませんが、ご回答をお待ちしております。
何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/14 10:35
退会済みユーザー
2018/03/19 13:51
2018/03/20 00:27 編集