ios - Swift - 如何获取嵌入在 UITableView 单元格中的选定控件段?

标签 ios swift uitableview uisegmentedcontrol

我在 UIViewController 中有一个 UITableView。有两个自定义单元格。其中之一有一个 UISegmentedControl。

enter image description here

到目前为止,还不错。

当我点击控件时,分段控件值发生变化,IBAction 函数按预期运行。

问题是所选索引始终显示为 -1,(也就是未选择任何内容)。

我在这里错过了什么?

这是值更改的代码:

 @IBAction func recurranceChanged(sender: UISegmentedControl?) {

        print ("index: ", recurrenceControl.selectedSegmentIndex) << This returns -1

        if recurrenceControl.selectedSegmentIndex == 0 {
            print("No ")
        }

        if recurrenceControl.selectedSegmentIndex == 1 {
            print("Sometimes ")

        }


        if recurrenceControl.selectedSegmentIndex == 2 {
            print("Yes")

        }

    }

最佳答案

试试这个:(也从 UISegmentedControl 中删除了 '?',因为它不需要。)

@IBAction func recurranceChanged(sender: UISegmentedControl) {

    print ("index: ", sender.selectedSegmentIndex)

    if sender.selectedSegmentIndex == 0 {
        print("No ")
    }

    if sender.selectedSegmentIndex == 1 {
        print("Sometimes ")

    }


    if sender.selectedSegmentIndex == 2 {
        print("Yes")

    }

}

这应该可以解决问题;)

关于ios - Swift - 如何获取嵌入在 UITableView 单元格中的选定控件段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38488070/

相关文章:

ios - 单击更改多个 UIButton 的背景颜色

ios - 无法用存储的属性覆盖 'description'

ios - Swift 框架不在 -swift 文件中生成 objective-c 头代码

iphone - iOS 6 : UITableView Edit and Autoresizing

ios - iOS 上的 AWSDynamoDBObjectMapper 将 Bool 值设置为 1 而不是 true?

swift - 根据最新的时间戳加载 tableView

ios - UItableviewcell "cell-identifier"内存管理

ios - 从函数中的数组返回一个项目

ios - 如何通过phonegap build使用iTunes文件导入/导出功能

ios - 如何让tableview控件适合整个屏幕