Swift의 TableView 화면 변환 편
13622 단어 Swift
http://qiita.com/senseiswift/items/9b5476531a843b0e314a
Swift의 간단한 표 보기 화면 변환 편
할 일
3. 새 파일 SecondViewController 추가
6. StoryBoard의 ViewController와 SecondeviewController를 연결하고 Identifer를'Show SecondView', Segue를'Show'로 설정
ViewController
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
// 7. SecondViewに渡す文字列
var selectedText: String?
// テーブルに表示するテキスト
let texts = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// セルの行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return texts.count
}
// セルのテキストを追加
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.textLabel?.text = texts[indexPath.row]
return cell
}
func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
println(texts[indexPath.row])
// 8. SecondViewControllerに渡す文字列をセット
selectedText = texts[indexPath.row]
// 8. SecondViewControllerへ遷移するSegueを呼び出す
performSegueWithIdentifier("showSecondView",sender: nil)
}
// Segueで遷移時の処理
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showSecondView") {
let secondVC: SecondViewController = (segue.destinationViewController as? SecondViewController)!
// 11. SecondViewControllerのtextに選択した文字列を設定する
secondVC.text = selectedText
}
}
}
SecondViewControllerimport UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
// 9. ViewControllerから受け取る文字列を入れる変数
var text: String?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// セルのテキストを追加
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
// 10. 受け取った文字列をセルに表示
cell.textLabel?.text = text
return cell
}
func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
println(text)
}
}
참조 소스https://github.com/senseiswift/tableviewtest/blob/master/tableviewtest/ViewController.swift
계속해서 Swift에서 TableView modal 화면 숨기기
http://qiita.com/senseiswift/items/90be98960b4c9dffe6aa
Reference
이 문제에 관하여(Swift의 TableView 화면 변환 편), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/senseiswift/items/20d09c523772caaf8005텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)