Reputation: 1433
I like to change text color to green
for an event, if eventStatus = 1
, and red
if the eventStatus = 2
. I tried the following and it is changing every event
to green
if the last selected
eventStatus = 1
and every event
to red
if the last selected
eventStatus = 2
. How do I display right colors according to eventStatus for every individual event?
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{}
}
Upvotes: 0
Views: 85
Reputation: 72460
You need to create array of EventSchedule
instead of creating two array and use that array with your cellForRowAt
method.
var eventSchedule = [EventSchedule]()
Now initialized this array in your eventData method.
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{}
}
Now use this single array with your cellForRowAt
method.
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
}
}
Upvotes: 1
Reputation: 7405
You loop through the fetchRequest
in cellForRowAt
so the color of the eventLabel
will be set according to the eventStatus
of the last result
. You should store the fetchRequest
as a global variable.
Solution:
Declare a global array for storing EventDec
objects:
var events: [EventDec] = []
Then, in eventData()
, insert the data into the array:
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)
}
Finally, in cellForRowAtIndexPath
, set the textColor
corresponding to the eventStatus
of the event:
if (events[indexPath.row].eventStatus == 1)
{
myEventCell.eventLabel?.textColor = UIColor.green
}
else
{
myEventCell.eventLabel?.textColor = UIColor.red
}
Upvotes: 0
Reputation: 123
That's right. The indexPath.row
isn't matching with your fetch results since it's effectively making a loop inside a loop due to your forEach
function. The last result of the forEach loop will be the result taken for all the cells. A solution could be making a cell.tag
for all your cells according to your eventStatus
and apply the colors from the cell.tag
value.
Upvotes: 0