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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

3524閲覧

swift Storyboardを使わないUITableViewページ遷移について

trox

総合スコア37

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2015/11/27 02:51

iOSアプリ制作にてStoryboardを使わずUITableViewのページ遷移について質問いたします。

現在のコードではどのテーブルを選択し遷移しても、遷移後ページに配列の最後の値が挿入されます。

AppDelegate.swift

swift

1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 8 var sendImage: UIImage? 9 var sendLabel: String? 10 11 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 12 13 let myFirstViewController: FirstViewController = FirstViewController() 14 self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 15 self.window?.rootViewController = myFirstViewController 16 self.window?.makeKeyAndVisible() 17 18 return true 19 } 20 21 func applicationWillResignActive(application: UIApplication) { 22 } 23 24 func applicationDidEnterBackground(application: UIApplication) { 25 } 26 27 func applicationWillEnterForeground(application: UIApplication) { 28 } 29 30 func applicationDidBecomeActive(application: UIApplication) { 31 } 32 33 func applicationWillTerminate(application: UIApplication) { 34 } 35 36 37}

FirstViewController.swift

swift

1import UIKit 2 3class FirstViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 4 5 private var myScrollView: UIScrollView! 6 7 private let myItems: NSArray = ["test1", "test2", "test3"] 8 private let myImages: NSArray = ["img1.jpg", "img2.jpg", "img3.jpg"] 9 private var myTableView: UITableView! 10 11 12 override func viewDidLoad() { 13 super.viewDidLoad() 14 15 16 // ScrollView 17 myScrollView = UIScrollView() 18 myScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) 19 20 let myImage = UIImage(named: "img8.jpg")! 21 let myImageView = UIImageView() 22 myImageView.image = myImage 23 myImageView.contentMode = UIViewContentMode.ScaleAspectFill 24 myImageView.frame = CGRectMake(0, 0, self.view.bounds.width, 200) 25 26 myScrollView.addSubview(myImageView) 27 myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height) 28 29 self.view.addSubview(myScrollView) 30 31 32 // TableView 33 let displayWidth: CGFloat = self.view.frame.width 34 let displayHeight: CGFloat = self.view.frame.height 35 36 myTableView = UITableView(frame: CGRect(x: 0, y: 200, width: displayWidth, height: displayHeight)) 37 myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 38 myTableView.dataSource = self 39 myTableView.delegate = self 40 41 self.view.addSubview(myTableView) 42 43 44 // Pageing 45 let nextButton: UIButton = UIButton(frame: CGRectMake(0, 0, 120, 50)) 46 nextButton.backgroundColor = UIColor.redColor() 47 nextButton.layer.masksToBounds = true 48 nextButton.setTitle("Next", forState: .Normal) 49 nextButton.layer.cornerRadius = 20.0 50 nextButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: 400) 51 nextButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) 52 53 self.view.addSubview(nextButton) 54 55 } 56 57 override func didReceiveMemoryWarning() { 58 super.didReceiveMemoryWarning() 59 // Dispose of any resources that can be recreated. 60 } 61 62 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 63 let mySubViewController: UIViewController = SubViewController() 64 mySubViewController.modalTransitionStyle = UIModalTransitionStyle.PartialCurl 65 self.presentViewController(mySubViewController, animated: true, completion: nil) 66 } 67 68 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 69 return myItems.count 70 } 71 72 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 73 let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) 74 cell.textLabel!.text = "\(myItems[indexPath.row])" 75 76 let image: UIImage! = UIImage(named: "\(myImages[indexPath.row])") 77 cell.imageView!.image = image 78 79 let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 80 appDelegate.sendImage = image 81 appDelegate.sendLabel = "\(myItems[indexPath.row])" 82 83 return cell 84 } 85 86}

SubViewController.swift

swift

1import UIKit 2 3class SubViewController: UIViewController { 4 5 private var myImageView: UIImageView! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 let backButton: UIButton = UIButton(frame: CGRectMake(0, 0, 120, 50)) 11 backButton.backgroundColor = UIColor.greenColor() 12 backButton.layer.masksToBounds = true 13 backButton.setTitle("Back", forState: .Normal) 14 backButton.layer.cornerRadius = 20.0 15 backButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: 400) 16 backButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside) 17 18 self.view.addSubview(backButton) 19 20 let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 21 22 // ImageView 23 myImageView = UIImageView(frame: CGRectMake(0, 0, 200, 300)) 24 let myImage = appDelegate.sendImage 25 myImageView.image = myImage 26 myImageView.contentMode = UIViewContentMode.ScaleAspectFill 27 myImageView.layer.position = CGPoint(x: self.view.bounds.width/2, y: 200) 28 29 self.view.addSubview(myImageView) 30 31 let label: UILabel! = UILabel(frame: CGRectMake(0, 0, 200, 50)) 32 label.text = appDelegate.sendLabel 33 label.textColor = UIColor.whiteColor() 34 label.layer.position = CGPoint(x: 200, y: 500) 35 self.view.addSubview(label) 36 37 } 38 39 40 override func didReceiveMemoryWarning() { 41 super.didReceiveMemoryWarning() 42 } 43 44 // Button Event 45 internal func onClickMyButton(sender: UIButton) { 46 let myViewController: UIViewController = FirstViewController() 47 myViewController.modalTransitionStyle = UIModalTransitionStyle.PartialCurl 48 self.presentViewController(myViewController, animated: true, completion: nil) 49 } 50}

ご教授いただけると助かります。どうぞよろしくお願い致します。

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

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

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

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

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

guest

回答1

0

ベストアンサー

cellForRowAtIndexPath(セル生成)の中にある、

swift

1let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 2appDelegate.sendImage = image 3appDelegate.sendLabel = "\(myItems[indexPath.row])" 4

この処理は、didSelectRowAtIndexPath(セルを選択)の中に書かないとダメですね。

投稿2015/11/27 02:59

fuzzball

総合スコア16731

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

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

trox

2015/11/27 03:07

解決できました! 素早く的確なご回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問