質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

1回答

2105閲覧

TableViewに記入した値の参照方法と、あらかじめTableViewへデータを表示させる方法を教えてください。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2018/03/19 13:46

編集2018/03/22 00:04

使用環境

PC:MaxOSX
環境:Visual Studio Community 2016 for mac
言語:c#

やりたいこと

  1. TableViewに手で直接文字列を入力し、その入力した文字列の中から、選択行にある文字列を参照したい。
  2. あらかじめ保存していたテキストデータなどをTableViewに表示したい。

1について

前提

状況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には空の文字列が入ってしまいます

最後に

どうすれば「やりたい事」が実現できるのかわからず、未だ途方に暮れております。
以前にご回答くださった方のおかげでだいぶ前進したのですが、なにぶん初心者すぎて右も左もわからぬ状態です。
何か足りない情報などございましたら、お声掛けくだされば追記いたします。

何卒皆様のお知恵をお貸しください。
ご回答をお待ちしております。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

データソース(NSTableViewDataSource)をテーブル(NSTableView)に設定できていなければ、テーブルにデータ(文字列)は表示されません。ですので、まずは正しくデータソースを設定できるようにする必要があります。

公式のシンプルなサンプルを確認するのと、ご自身のソースコードとの違いを確認していただくといいんじゃないでしょうか。

投稿2018/03/27 00:27

ukgraphics

総合スコア69

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問