【问题标题】:How to create matrix survey use swift?如何快速创建矩阵调查?
【发布时间】:2015-01-09 09:52:44
【问题描述】:

我已经为矩阵调查创建了 UI 布局,但是如何让它每行只回答一个呢?

请更正我的代码,使其每行只允许一个答案。 这是我创建的视图控制器,我以编程方式使行循环。

这是我的快速代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as QMultiOptionCell

    let idx = indexPath.row
    if selectedAnswers.containsObject(idx) {
        cell.option1CheckImgView.image = UIImage(named: "circledot_uns")
        cell.option2CheckImgView.image = UIImage(named: "circledot_uns")
        cell.option3CheckImgView.image = UIImage(named: "circledot_uns")
        cell.option4CheckImgView.image = UIImage(named: "circledot_uns")
        cell.option5CheckImgView.image = UIImage(named: "circledot_uns")
        selectedAnswers.removeObject(idx)
    } else {
        cell.option1CheckImgView.image = UIImage(named: "circledot")
        cell.option2CheckImgView.image = UIImage(named: "circledot")
        cell.option3CheckImgView.image = UIImage(named: "circledot")
        cell.option4CheckImgView.image = UIImage(named: "circledot")
        cell.option5CheckImgView.image = UIImage(named: "circledot")
        selectedAnswers.addObject(idx)
    }
    cell.selected = false
}

这是错误截图。当我单击一个 circledot 时,所有 circledot 都变为活动状态:

该代码使所有圆点一起回答,我只希望每一行只回答一个圆点,就像http://www.surveymonkey.com/r/?sm=DnPrQVbiWt6RgarN6lUkkQ%3d%3d 中的选择矩阵(允许一个答案)一样。 问候。

【问题讨论】:

  • 您想从单元格中选择一个选项吗?
  • 对,当我连续单击一个圆点时,另一个圆点变为 circledot_uns。但我认为我的代码问题是,那是一行代码。如果单击该行中的圆点,则该行(同一行)中的其他圆点也将变为圆点(活动)。我希望帮助修复该代码。

标签: ios uitableview swift


【解决方案1】:

您无法使用当前实现来实现预期的行为。

问题 1

在您的didSelectRowAtIndexPath: 中,您正在设置特定单元格上的所有按钮,因此所选行中的每个单元格都将由于此代码而被选中。

问题 2

你不能轻易使用didSelectRowAtIndexPath:来达到这个目的,因为你不知道用户点击了哪个按钮(为此你需要计算触摸位置并找到按钮)。

解决方案:

不要依赖didSelectRowAtIndexPath:,而是为单元格中添加的按钮添加一个操作。当用户单击按钮时,找到单元格(使用superView 属性),然后找到该单元格的indexPath(使用UITableViewCell,您从上一步中获得)。如果该 indexPath 不在您的数组中,则应清除上一个单元格按钮(可以使用相同的代码),然后选择被单击的按钮(使用按钮操作的 sender 参数)。如果 indexPath 在您的数组中,则先清除所有按钮选择,然后选择单击的按钮。

【讨论】:

  • 您能用代码解释一下您的解决方案吗?因为我不懂
  • @user1858725:您在哪个区域遇到问题?
  • 在这里:为单元格中添加的按钮添加操作。当用户单击按钮时,找到单元格(使用 superView 属性),然后找到该单元格的 indexPath(使用 UITableViewCell,您从上一步中获得),您的意思是使用按钮而不是 uiimage?
  • @user1858725:为所有这些按钮创建一个 IBAction(所有方法相同)
  • 像这样:@IBAction func tombol1change(sender: UIButton) { println(1) sender.setBackgroundImage(UIImage(named: "circledot"), forState: UIControlState.Normal) func tableView(tableView: UITableView , didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) as QMultiOptionCell let idx = indexPath.row cell.selected = false } } 现在每次我点击按钮时它都会变成圆点但是当我点击同一个按钮时它会保持圆点(不是 circledot_uns)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
相关资源
最近更新 更多