在回调中使用函数/在单元格内使用分段控件

问题描述:

我对Swift比较陌生,所以请和我一起裸照。在回调中使用函数/在单元格内使用分段控件

我有一个在每个UITableViewCell中UISegmentedControls一个应用程序,它看起来像这样:

https://i.stack.imgur.com/ezMUJ.png

对于每一个用户在其中选择“是”的选项行,我想打印索引该行。

在这种情况下,第一步是在UISegmentedControl上使用addTarget来指定处理分段控件操作的函数。但是,由于我使用的是自定义分段控件,因此addTarget不可用(回调和委派是将信息传递到分段控件的唯一方法)。

我的第一个问题是,有没有办法在回调中调用方法? 我的第二个问题是,是否有一个更简单的方法来做到这一点?有些人建议在单元和视图控制器之间传输数据的协议和委托,但我不确定如何做到这一点或者它是否合适。

这里是我的代码至今:

进口的UIKit

类MyTableViewController:的UITableViewController {

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 150 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
    return 10 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) 
    cell.backgroundColor = .green 
    return cell 

} 

func printCellIndexPath(cell: UITableViewCell){ 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.register(MyCell.self, forCellReuseIdentifier: "cellId") 

    // 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. 
} 

}

类了myCell:UITableViewCell的{

var myTableViewController: MyTableViewController? 

override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    setupViews() 

} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 


let nameLabel: UILabel = { 
    let label = UILabel() 
    label.text = "Sample Item" 
    label.translatesAutoresizingMaskIntoConstraints = false 
    return label 
    }() 

let actionButton = YSSegmentedControl(
    frame: CGRect.zero, 
    titles: [ 
     "Yes", 
     "No" 
    ], 
    action: { 
     control, index in 
     if index == 1 { 
      print ("tapped") 
     } 
     else{ 

     } 

}) 

func setupViews(){ 


    addSubview(nameLabel) 
    addSubview(actionButton) 
    actionButton.translatesAutoresizingMaskIntoConstraints = false 

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]-160-[v1]-16-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel, "v1": actionButton])); 
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])) 
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-60-[v0]-60-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": actionButton])) 

} 

}

其中MyCell是我的自定义单元类,actionButton是每个单元格中存在的UISegmentedControl。

感谢, 尼克

要删除代表团和回调为什么不ü使YSSegmentedControl中的cellForRowAtIndexPath方法和有ü将通过标签来YSSegmentedControl的对象一样,

actionButton.tag = indexpath.row

和当u点击任何段U将得到在YSSegmentedControl代表回调这是

FUNC segmentedControl(_ segmentedControl:YSSegmentedControl, willPressItemAt指数:智力){ 打印( “(segmentedControl.tag)” + “(索引)”) }

func segmentedControl(_ segmentedControl: YSSegmentedControl, didPressItemAt index: Int){ 

} 

从这些代表方法你将通过 segmentedControl得到YSSegmentedControl行。标签和特定按钮索引通过索引

+0

这工作完美。非常感谢。 –