自定义单元格中的按钮动作

问题描述:

我没有找到任何正常的解决方案,如何检测点击按钮或其他UIViewController中的行为与@IBOutlet weak var tableView:UITableView!与自定义单元格。自定义单元格中的按钮动作

我的手机:

class CartTableViewCell: UITableViewCell, FloatRatingViewDelegate { 
    @IBOutlet var floatRatingView: FloatRatingView! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
     self.floatRatingView.delegate = self 
    } 

    // MARK: FloatRatingViewDelegate 

    func floatRatingView(ratingView: FloatRatingView, didUpdate rating: Float) { 


    } 
} 

如何floatRatingView方法来访问,从我的观点的变量?

有两种方法可以完成。

第一种是使用界面生成器,有一个引导的位置: https://developer.apple.com/library/ios/recipes/xcode_help-IB_connections/chapters/CreatingAction.html

另一种选择是要做到这一点编程

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
    self.floatRatingView.delegate = self 

    myButton.addTarget(self, action: "myButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside) 
} 

func myButtonTapped(sender: UIButton) { 
    floatRatingView(someView, didUpdate: someRating) 
} 

func floatRatingView(ratingView: FloatRatingView, didUpdate rating: Float) { 

} 
+0

哪个更好??? – Arti

+0

只取决于你的项目和使用。如果您是以编程方式创建按钮,或者想要更改按钮调用的功能,请使用第二种方法。如果在界面构建器/故事板中有大部分布局,则使用第一种方法。 –