如何自动选择UITableview单元格



我正在一个测验应用程序上工作。我从API得到问题。我用tableview作为选项。

现在当用户选择第一个问题的答案&下一步是前一个问题。然后选中的答案必须保持选中。

我研究了很多,发现了这个:

在Swift中编程模拟UITableViewController中的选择

但是我不能在我的表视图中自动选择用户选择的答案。

This is my Present UI

func getOptions(){
    OptionArray.removeAll(keepCapacity: false)
    Alamofire.request(.GET, "http://www.wins.com/index.php/capp/get_chapter_answers/(EID)/(QuestionID[Qindex])")
        .responseJSON { (_, _, data, _) in
            println(data)
            let json = JSON(data!)
            let catCount = json.count
            for index in 0...catCount-1 {
                let disp = json[index]["DISPLAY_STATUS"].string
                if disp == "Y"{
                let op = json[index]["ANSWER"].string
                self.OptionArray.append(op!)
                let ans = json[index]["RIGHT_ANSWER"].string
                self.AnswerArray.append(ans!)
                }
            }
            self.OptionTable.reloadData()
            println(self.OptionArray.count)
    }
}
@IBAction func Previous(sender: AnyObject) {
    Qindex--
    ShowQuestion()
}

@IBAction func Next(sender: AnyObject) {
    Qindex++
    ShowQuestion()
   }

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.OptionArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.OptionTable.dequeueReusableCellWithIdentifier("Option") as! OptionCell
    cell.Optionlabel?.text = self.OptionArray[indexPath.row]
    cell.layer.masksToBounds = true;
    cell.layer.cornerRadius = 6;
    cell.layer.borderWidth = 2.0
    cell.layer.borderColor = colorsArray[1].CGColor
    return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = tableView.indexPathForSelectedRow();
    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! OptionCell;
    if currentCell.selected == true{
        currentCell.layer.borderWidth = 4.0
        currentCell.layer.borderColor = colorsArray[6].CGColor
        println(currentCell.Optionlabel?.text)
    }
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! OptionCell;
    if currentCell.selected == false{
        currentCell.layer.borderWidth = 2.0
        currentCell.layer.borderColor = colorsArray[1].CGColor
    }
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 70
}

  1. 我有20多个问题。所以我必须单独保存每个问题的选择答案。

  2. 我不能使用索引位置选择答案,因为选项会在第二次访问时随机改变它的位置。

你可以这样做:当您按下next时,将所选答案的索引存储到一个变量中,当您回到previous时,在willDisplayCell方法中检查该索引并设置您的单元格所选。

在控制器中取一个变量

var selectedAnsIndexPath:NSIndexPath?

你的下一个按钮动作将是类似于

@IBAction func Next(sender: AnyObject) {
   self.selectedAnsIndexPath = tableView.indexPathForSelectedRow()
    Qindex++
    ShowQuestion()
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if(indexPath == self.selectedAnsIndexPath) 
    {
        cell.setSelected(true, animated: false)
    }
}

试试这个,它可能对你有用!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = self.OptionTable.dequeueReusableCellWithIdentifier("Option") as! OptionCell 
cell.Optionlabel?.text = self.OptionArray[indexPath.row] 
cell.QueID = QuestionID[Qindex] 
cell.layer.masksToBounds = true; 
cell.layer.cornerRadius = 6; 
cell.layer.borderWidth = 2.0 
cell.layer.borderColor = colorsArray[1].CGColor 

if let val = examDic[cell.QueID] 
{ 
    if self.OptionArray[indexPath.row] == val 
    { 
        selectedAnsIndexPath = indexPath 
        cell.setSelected(true, animated: true) 
        cell.layer.borderWidth = 4.0 
        cell.layer.borderColor = colorsArray[6].CGColor 
    } 
} 
return cell 
} 
 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
if selectedAnsIndexPath != nil{ 
    OptionTable.deselectRowAtIndexPath(selectedAnsIndexPath!, animated: false) 
    self.tableView(OptionTable, didDeselectRowAtIndexPath: selectedAnsIndexPath!) 
    println(selectedAnsIndexPath!.row) 
} 
let indexPath = OptionTable.indexPathForSelectedRow(); 
let currentCell = OptionTable.cellForRowAtIndexPath(indexPath!) as! OptionCell; 
if currentCell.selected == true{ 
    currentCell.layer.borderWidth = 4.0 
    currentCell.layer.borderColor = colorsArray[6].CGColor 
    var sqid = QuestionID[Qindex] 
    var sanswer = currentCell.Optionlabel!.text 
    examDic[sqid] = sanswer! 
    println(examDic) 
} 
}

最新更新