2016-10-19 7 views
0

私のUITableViewControllerでは、DataSourceの外部クラスを設定しました。以下のようにUITableViewDataSourceからUITableViewCellのデリゲートを設定するにはどうすればよいですか?

class HomeViewDataSource: NSObject, UITableViewDataSource { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(myCellIdentifier, forIndexPath: indexPath) 
     cell.delegate = //Should I set this to the view controller? 
     return cell 
    } 
} 

ご覧のとおり、デリゲートを設定したい再利用可能なセルがあります。セルには、UITableViewControllerでイベントをトリガーするボタンがあります。今のところ、ネットワークの呼び出しを実行できるように、セルのデリゲートを自分のUITableViewControllerに設定しました。私は正しいMVC形式でこれをやっているとは思わない。

UITableViewControllerのネットワーク呼び出しでは、UITableViewDataSourceの配列のプロパティが使用されます。

これを行うにはどのような方法が便利ですか?

答えて

0

私はもうこれについて考えて、閉鎖を試みることに決めました。 ここに私がそれをする方法があります。

//MyCellClass: UITableViewCell 

@IBAction func buttonPressed(sender:UIButton) { 
    buttonNetworkCall?(self) 
} 

とUITableViewDataSourceで

//HomeViewDataSource: NSObject, UITableViewDataSource { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(myCellIdentifier, forIndexPath: indexPath) 

    cell.buttonNetworkCall = { (cell) in 
     self.makeButtonNetworkCall(cell) 
    } 

    return cell 
    } 
} 
関連する問題