ios - 在回调中使用函数/在单元格内使用 SegmentedControl

标签 ios swift uitableview swift3 callback

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

/image/ezMUJ.png

对于用户选择"is"选项的每一行,我想打印该行的索引。

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

我的第一个问题是,有没有办法在回调中调用方法? 我的第二个问题是,是否有更简单的方法来完成此任务?有些人建议在单元和 View Controller 之间传输数据的协议(protocol)和委托(delegate),但我不太确定如何做到这一点或者它是否合适。

这是我到目前为止的代码:

导入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。

最佳答案

要删除委托(delegate)和回调,为什么不在 cellForrowatIndexpath 方法中创建 YSSegmentedControl,然后将标签传递给 YSSegmentedControl 的对象,就像这样

actionButton.tag = indexpath.row

当您单击任何分段时,您将在 YSSegmentedControl 委托(delegate)中收到回调

func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int){ print("(segmentedControl.tag)" + "(index)") }

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

}

从这些委托(delegate)方法中,您将通过以下方式获取 YSSegmentedControl 行 segmentedControl.tag 和通过 index 的特定按钮索引

关于ios - 在回调中使用函数/在单元格内使用 SegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44360808/

相关文章:

ios - Google Analytics for iOS v3 的 kGAIScreenName 未声明?

ios - Swift 中不显示警报 View ?

ios - 详细使用UITableView,如何在TextView中显示字符串(Master-Detail项目,Swift,iOS)

ios - 创建像 iOS 日历一样的 TableView

iphone - 使用 CGLayer 缓存 UITableView 中的单元格

ios - 计算类的对象数

android - Flutter 中使用 dart 的视频编辑器

ios - 从 iOS 中的 Web 服务获取故障代码

ios - TableView 中的 Collectionview - 如何选择项目?

ios - CALayer 不显示 iOS8