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

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

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

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

1回答

2064閲覧

[iphone]tableviewのcellを選択したらplistの内容を遷移先に表示する方法

pigretton

総合スコア27

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2015/02/11 03:32

![イメージ説明]WIDTH:600

xcode 6.0.1 でobjective-cを使い辞書アプリを製作しています。
内容はplistのkey"title"をテーブルビューのセルに貼り付けて、選択するとkey"contents"の文字を画面遷移先に表示するアプリです。 
前回、teratailでご助言いただき、plistから特定のkey<title>を取得してセルに並べる事が出来ました。ありがとうございます。
しかし、key<title>を選択して遷移した先のviewcontrollerにkey<title>と同じアイテムの中にあるkey<contents>を表示したいのですが、どのようなコードをtableviewやviewcontollerのファイルに追加すればよいでしょうか?
下記の参考にしているページではplistに含まれている画像の名前から画像を遷移先に表示するのですが、自分では応用できませんでした。

質問ばかりで大変恐縮ですが、ご助言お願い致します。
参考:https://akira-watson.com/iphone/tableview_3_objective-c.html

————PropertyList.plist——————————————————————————

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
<array>
<dict>
<key>title</key>
<string></string>
<key>contents</key>
<string>DNAを含有する</string>
</dict>
<dict>
<key>title</key>
<string>ミトコンドリア</string>
<key>contents</key>
<string>ATPを作る</string>
</dict>
<dict>
<key>title</key>
<string>ゴルジ体</string>
<key>contents</key>
<string>分泌に関与</string>
</dict>
<dict>
<key>title</key>
<string>葉緑体</string>
<key>contents</key>
<string>光合成を行う</string>
</dict>
<dict>
<key>title</key>
<string>液胞</string>
<key>contents</key>
<string>アントシアニンを含み浸透圧調節機能を持つ</string>
</dict>
</array>
</plist>
———————————————————————————————————————-

————TableViewController.h—————————————————

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>

@end

————————————————————————————————————————

————TableViewController.m—————————————————

#import "TableViewController.h"

@interface TableViewController (){
NSArray* arrayList;
}

@end

@implementation TableViewController

(void)viewDidLoad

{
[super viewDidLoad];
//plistのパスを取得してarrayの配列に入れる
NSString* dataFile = [[NSBundle mainBundle]pathForResource:@"PropertyList" ofType:@"plist"];
arrayList = [NSArray arrayWithContentsOfFile:dataFile];


}

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{
// セクションの数を1個にしました
return 1;
}

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
// 行数を10にしました
return 10;
}

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

NSDictionary *listDict = [arrayList objectAtIndex:indexPath.row];
cell.textLabel.text = [listDict objectForKey:@"title"];
cell.textLabel.textColor = [UIColor cyanColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.detailTextLabel.textColor = [UIColor lightGrayColor];

return cell;
}

// セルの背景色を変える
(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor clearColor];
}

@end

——————————————————————————————————————
――――――ViewController.h――――――――――――――――――――――――
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
――――――――――――――――――――――――――――――――――――――
――――――ViewController.m――――――――――――――――――――――――
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

@end
――――――――――――――――――――――――――――――――――――――

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

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

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

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

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

guest

回答1

0

ViewController.hに
@propaty NSDictionary*dic;//①

TableViewController.mに
tableView: didSelectRowAtIndexPath://②
メソッドを追加し、その中に

lang

1ViewController *vc = [ViewController new];//③' 2vc.dic = [arrayList objectAtIndex:indexPath.row];//③'' 3[self.navigationController pushViewController:vc animated:true];//③''' 4```//③ 5 6と実装してみてください。 7また、もしもStoryboard上で画面遷移を実装している場合はその部分は無効にしてください。 8ViewController内で、self.dicに選択した箇所の情報が入っているのでNSLogなどで確認してみてください。 9 10このコードは 11①でViewControllerにNSDictionary型のプロパティを実装しています。 12こうすることで、他のクラスからこのViewControllerのインスタンスにdicを受け渡す事ができます。 13②はTableViewControllerのセルをタップした際に呼ばれるメソッドです。これはUITableViewDelegateが提供しています。 14③は画面遷移のコードです。 15③'でViewControllerのインスタンスを生成し 16③''でそのインスタンスのdicプロパティに選択した箇所のplistの中身を入れています。 17③'''で画面遷移を実行しています。

投稿2015/02/11 04:34

noppefoxwolf

総合スコア231

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問