2015-11-24 35 views
1

我正在嘗試進行測驗,因此當您單擊nextButton時,它會檢查問題是否正確,如果是,則應該使單元格變綠,如果錯誤它應該使chosenCell紅色和correctCell綠色。然而,它似乎繼續重新設置標籤,而不是使單元正確的顏色。這裏是我選擇正確的一個例子。它使第一個單元格的textColor變爲白色,並重置正確的標籤?根據正確或錯誤測試更改單元格背景

插圖

enter image description here

的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

首先:您濫用出列機制。如果你想獲得一個已經存在的細胞,使用

let cell = tableView.cellForRowAtIndexPath(indexPath) 

其次,你應該永遠不會從其他地方比的tableView(_:cellForRowAtIndexPath:)內操縱細胞。原因是,如果UITableView決定重新載入其內容,則僅在上述方法之外完成的所有更改將會丟失。

您必須改變支持狀態並手動重新加載受影響的行。

你必須有改變你目前的邏輯是這樣的

if indexPath.section == 0 { 
    let cell = tableView.dequeueReusableCellWithIdentifier("ButtonCell", forIndexPath: indexPath) as! QuestionCell 

    cell.titleLabel.text = options[indexPath.row] 

    if let chosen = chosenIndex { 
     if chosen == indexPath.row { 
      if indexPath.row == correctIndex { 
       cell.backgroundColor = UIColor.greenColor() 
      } else { 
       cell.backgroundColor = UIColor.redColor() 
      } 
     } else { 
      if indexPath.row == correctIndex { 
       cell.backgroundColor = UIColor.greenColor() 
      } else { 
       cell.backgroundColor = UIColor.whiteColor() 
      } 
     } 
    } else { 
     cell.backgroundColor = UIColor.whiteColor() 
    } 
    return cell 
} 

在你func done()你那麼只需要告訴tableView正確的行:

  • 選擇和正確的,如果正確的單元格已被選中
  • 所選單元格和正確的單元格如果選擇錯誤

請注意,我遺漏了設置textColor屬性,因爲它們的邏輯類似於backgroundColor,我認爲你可以也應該弄清楚如何正確設置它們。

+0

但我想觸發這一點,當我點擊第1部分中的'nextButton' –

+0

@PeterPik我不明白:在什麼時候要調用它並不重要。 – luk2302

+0

@PeterPik是否適合你?總的來說,對於回答問題的人有用的答案留下積極的回答 - 看看你以前的問題表明你很少這樣做。 – luk2302

相關問題