2017-03-06 28 views
0

我喜歡將文字顏色更改爲​​作爲事件,如果eventStatus = 1red如果eventStatus = 2。我嘗試了以下方法,它將every event更改爲​​如果last selectedeventStatus = 1every eventred如果last selectedeventStatus = 2。如何根據每個單獨事件的eventStatus顯示正確的顏色?通過cellForRowAtfetchRequest所以eventLabel的顏色會根據最後resulteventStatus設置根據選擇顯示正確的文字顏色?

override func viewDidLoad() { 
     super.viewDidLoad() 
     eventData() 
     for i in stride(from: 0, to: eventTitleOnc.count, by: 1){ 
       oncEvents.append(eventTitleOnc[i])} 

    for i in stride(from: 0, to: eventTitleOfc.count, by: 1){ 
     ofcEvents.append(eventTitleOfc[i]) 
    myEventTableView.reloadData() 
} 


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
let myEventCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! EventTableViewCell 

    switch (eventSegCtrl.selectedSegmentIndex) 
    { 
    case 0: 
     myEventCell.eventLabel.text = oncEvents[indexPath.row] 
     break 
    case 1: 
     myEventCell.eventLabel.text = ofcEvents[indexPath.row] 
     break 
default:break 
    } 
    let fetchRequest: NSFetchRequest<EventDec> = EventDec.fetchRequest() 
    if let results = try? context.fetch(fetchRequest) { 
     results.forEach { (result) in 
      if (result.eventStatus == 1) 
      { 
       myEventCell.eventLabel?.textColor = UIColor.green 
      } 
      else 
      { 
       myEventCell.eventLabel?.textColor = UIColor.red 
      } 
     } 
    } 
    return myEventCell 
} 

private func eventData(){ 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     let context = appDelegate.persistentContainer.viewContext 
     let request = NSFetchRequest<NSFetchRequestResult>(entityName: "EventSchedule") 
     request.returnsObjectsAsFaults = false 
     do 
     { 
      let results = try context.fetch(request) 
      if results.count > 0 
      { 
       for result in results as! [NSManagedObject] 
       { 
        if let eventDisTextOnc = result.value(forKey: "eventNameOnc") as? String 
        { 
         eventTitleOnc.append(eventDisTextOnc) 
        } 
        if let eventDisTextOfc = result.value(forKey: "eventNameOfc") as? String 
        { 
         eventTitleOfc.append(eventDisTextOfc) 
        } 
       } 
      } 
     } 
     catch{} 
    } 
+0

如何更改'last selected eventStatus'? – chengsam

+0

我有一個動作控制器,如果用戶選擇是,它的eventStatus = 1,如果不是,它的eventStatus = 2 – Coder221

+0

你的代碼看起來很好,你可以確認當用戶選擇yes/no時更新'fetchRequest'嗎? – chengsam

回答

1

您需要創建EventSchedule的數組,而不是創建兩個數組,並將該數組與您的cellForRowAt方法一起使用。

var eventSchedule = [EventSchedule]() 

現在在你的eventData方法中初始化這個數組。

private func eventData(){ 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let context = appDelegate.persistentContainer.viewContext 
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "EventSchedule") 
    request.returnsObjectsAsFaults = false 
    do 
    { 
     self.eventSchedule = try context.fetch(request) as! [EventSchedule] 

    } 
    catch{} 
} 

現在使用這個單一的陣列與您的cellForRowAt方法。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
    let myEventCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! EventTableViewCell 

    if eventSegCtrl.selectedSegmentIndex == 0 { 
     myEventCell.eventLabel.text = self.eventSchedule[indexPath.row]. eventNameOnc 
    } 
    else { 
     myEventCell.eventLabel.text = self.eventSchedule[indexPath.row]. eventNameOfc 
    } 
    if let eventDesc = self.eventSchedule[indexPath.row].eventDec,   
     let event = eventDesc allObjects.last as? EventDec, event.eventStatus == 1 { 
     myEventCell.eventLabel.textColor = UIColor.green 
    } 
    else { 
     myEventCell.eventLabel.textColor = UIColor.red 
    } 
} 
0

你循環。您應該將fetchRequest作爲全局變量存儲。

聲明全局陣列,用於存儲EventDec對象:

var events: [EventDec] = [] 

然後,在eventData(),將數據插入到所述陣列:

for result in results as! [NSManagedObject] 
{ 
    if let eventDisTextOnc = result.value(forKey: "eventNameOnc") as? String 
    { 
     eventTitleOnc.append(eventDisTextOnc) 
    } 
    if let eventDisTextOfc = result.value(forKey: "eventNameOfc") as? String 
    { 
     eventTitleOfc.append(eventDisTextOfc) 
    } 

    events.append(result as! EventDec) 
} 

最後,在cellForRowAtIndexPath,設置textColor對應eventStatus

if (events[indexPath.row].eventStatus == 1) 
{ 
    myEventCell.eventLabel?.textColor = UIColor.green 
} 
else 
{ 
    myEventCell.eventLabel?.textColor = UIColor.red 
} 
+0

我這樣做了,但它不起作用。 – Coder221

+0

你可以發佈代碼嗎? – chengsam

+0

哪部分代碼? – Coder221

0

沒錯。 indexPath.row與您的讀取結果不匹配,因爲您的forEach函數有效地在循環內部形成循環。 forEach循環的最後結果將是所有單元格的結果。解決方案可以根據您的eventStatus爲所有單元製作cell.tag,並應用cell.tag值中的顏色。

+0

欣賞你的解釋,你能否詳細說明一些有代碼的解釋或例子 – Coder221

+0

我沒有你寫的全部代碼,所以我可以給你一個更簡短的方法。只需調整您的獲取請求以通過匹配我假設的事件的名稱或任何唯一標識符來獲取該特定單元的內容,然後對您的if和else語句執行相同的操作。 '如果(result.eventStatus == 1) { myEventCell.eventLabel?.textColor = UIColor.green } 別的 { myEventCell.eventLabel?.textColor = UIColor.red }' – CoderSulemani