Last active
July 12, 2018 08:31
-
-
Save rmzaki/ad0f636a1b8cd3e0a9541c3ac3890e7f to your computer and use it in GitHub Desktop.
カスタムセル上のボタン押下時にどのセルが選択されたか取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
/** | |
ViewController | |
*/ | |
class ViewController: UIViewController { | |
// テーブルビュー | |
@IBOutlet weak var tableView: UITableView! | |
// テーブルの上のラベル | |
@IBOutlet weak var titleLabel: UILabel! | |
// ラベルに表示するデータ | |
let labelTexts: [String] = ["えび味噌ラーメン", "豚骨ラーメン", "塩ラーメン", "味噌ラーメン", "醤油ラーメン"] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
/** | |
食べるボタンが押されたとき | |
- parameter sender: 食べるボタン | |
*/ | |
@IBAction func pushCellButton(sender: UIButton) { | |
let cell = sender.superview?.superview as! CustomCell | |
guard let row = self.tableView.indexPathForCell(cell)?.row else { | |
return | |
} | |
self.titleLabel.text = self.labelTexts[row] + "\n(\(row)番目のボタンが押されました)" | |
} | |
} | |
/** | |
テーブルの設定 | |
*/ | |
extension ViewController: UITableViewDataSource { | |
// セクション数 | |
func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return 1 | |
} | |
// セクションの行数 | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return self.labelTexts.count | |
} | |
// セルの設定 | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell | |
cell.label.text = self.labelTexts[indexPath.row] | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment