我正在嘗試進行測驗,因此當您單擊nextButton
時,它會檢查問題是否正確,如果是,則應該使單元格變綠,如果錯誤它應該使chosenCell
紅色和correctCell
綠色。然而,它似乎繼續重新設置標籤,而不是使單元正確的顏色。這裏是我選擇正確的一個例子。它使第一個單元格的textColor變爲白色,並重置正確的標籤?根據正確或錯誤測試更改單元格背景
插圖
的cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("ButtonCell", forIndexPath: indexPath) as! QuestionCell
cell.titleLabel.text = options[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("NextCell", forIndexPath: indexPath) as! NextCell
if colorCheck > 186 {
cell.nextButton.backgroundColor = UIColor(rgba: quizObject!.navButtonsColor)
} else {
cell.nextButton.backgroundColor = UIColor(rgba: quizObject!.navBarColor)
}
cell.nextButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
cell.nextButton.addTarget(self, action: "done", forControlEvents: UIControlEvents.TouchUpInside)
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
}
didSelectRowAtIndexPath方法,做
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
chosenIndex = indexPath.row
}
func done() {
if chosenIndex != nil {
tableView?.deselectRowAtIndexPath(NSIndexPath(forRow: chosenIndex!, inSection: 0), animated: true)
let cell = tableView!.dequeueReusableCellWithIdentifier("ButtonCell", forIndexPath: NSIndexPath(forRow: chosenIndex!, inSection: 0)) as! QuestionCell
cell.titleLabel.textColor = UIColor.whiteColor()
let cell2 = tableView!.dequeueReusableCellWithIdentifier("ButtonCell", forIndexPath: NSIndexPath(forRow: correctIndex, inSection: 0)) as! QuestionCell
cell2.titleLabel.textColor = UIColor.whiteColor()
if chosenIndex! == correctIndex {
cell.backgroundColor = UIColor.greenColor()
} else {
cell.backgroundColor = UIColor.redColor()
cell2.backgroundColor = UIColor.greenColor()
}
tableView?.reloadData()
}
}
但我想觸發這一點,當我點擊第1部分中的'nextButton' –
@PeterPik我不明白:在什麼時候要調用它並不重要。 – luk2302
@PeterPik是否適合你?總的來說,對於回答問題的人有用的答案留下積極的回答 - 看看你以前的問題表明你很少這樣做。 – luk2302