如何使用swift创建矩阵调查?

时间:2015-01-09 09:52:45

标签: ios uitableview swift

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

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

http://i61.tinypic.com/nzpoc8.png http://i61.tinypic.com/nzpoc8.png 这是我的快捷代码:

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
}

这是错误截图。当我点击一个圆圈时,所有圆圈都变得活跃: http://i57.tinypic.com/97pij9.png http://i57.tinypic.com/97pij9.png

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

1 个答案:

答案 0 :(得分:0)

您无法通过当前实施来实现预期的行为。

第1期

didSelectRowAtIndexPath:您正在设置特定单元格上的所有按钮,因此将根据此代码选择所选行中的每个单元格。

第2期

您无法轻松使用didSelectRowAtIndexPath:用于此目的,因为您无法知道用户点击的按钮(为此您需要计算触摸位置并找到按钮)。< / p>

解决方案:

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