viewcontorollerとtableviewのdatasourceを分離して実装しているのですが、delegateで通知を受け取ることができません。
cell上のボタンをタップしたときに(datasource側)viewconに対して選択したボタンの内容を送るようにしたいと思っています。
datasourceからviewconにデリゲートで通知を送るにはどうすれば良いか教えてください。
現在の実装でボタンをタップのアクション検知はできています。
よろしくお願いいたします。
TestDatasource.swift
swift
1import Foundation 2import UIKit 3 4@objc protocol TestDelegate { 5 func sendButtonInfo(sender: UIButton) 6} 7 8class TestDatasource: NSObject, UITableViewDataSource { 9 10 weak var delegate: TestDelegate? 11 12 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell { 13 let cellIdentifier = "TestCell" 14 let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 15 cell.button.addTarget(self, action: "tapButton:", forControlEvents: UIControlEvents.TouchUpInside) 16 return cell 17 } 18 19 func tapButton(sender: UIButton!) { 20 self.delegate?.sendButtonInfo(sender) 21 } 22 23}
TestViewcontroller.swift
swift
1import Foundation 2 3class TestViewController : UIViewController, IndicatorInfoProvider, TestDelegae { 4 5 private var dataSource = TestDatasource() 6 @IBOutlet weak var testTableView: UITableView! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 self.testTableView.dataSource = self.dataSource 11 self.testTableView.delegate = self.dataSource 12 } 13 14 func sendButtonInfo(sender: UIButton) { 15 print("sendButtonInfo") 16 } 17 18}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。