我喜歡將文字顏色更改爲作爲事件,如果eventStatus = 1
和red
如果eventStatus = 2
。我嘗試了以下方法,它將every event
更改爲如果last selected
eventStatus = 1
和every event
到red
如果last selected
eventStatus = 2
。如何根據每個單獨事件的eventStatus顯示正確的顏色?通過cellForRowAt
的fetchRequest
所以eventLabel
的顏色會根據最後result
的eventStatus
設置根據選擇顯示正確的文字顏色?
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{}
}
如何更改'last selected eventStatus'? – chengsam
我有一個動作控制器,如果用戶選擇是,它的eventStatus = 1,如果不是,它的eventStatus = 2 – Coder221
你的代碼看起來很好,你可以確認當用戶選擇yes/no時更新'fetchRequest'嗎? – chengsam