Coder221 Coder221 - 8 months ago 38
Swift Question

Display right text color according to segment?

I have a segmented control with 2 segments

ONC
and
OFC
, if I change color of
row1
under
ONC
, it is changing color of
row1
in
OFC
as well. I like to change text color to
green
for an event, if
eventStatus = 1
, and
red
if the
eventStatus = 2
. This is what I have done so far

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context) as! EventDec
var eventSchOnc: EventScheduleOnc?
var eventSchOfc: EventScheduleOfc?
do {
switch (eventSegCtrl.selectedSegmentIndex)
{
case 0:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EventScheduleOnc")
fetchRequest.fetchLimit = 1
fetchRequest.predicate = NSPredicate(format: "eventNameOnc == %@", self.oncEvents[indexPath.row] as String)
var objects: [EventScheduleOnc]
try objects = context.fetch(fetchRequest) as! [EventScheduleOnc]
eventSchOnc = objects[0] as EventScheduleOnc
break

case 1:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EventScheduleOfc")
fetchRequest.fetchLimit = 1
fetchRequest.predicate = NSPredicate(format: "eventNameOfc == %@", self.ofcEvents[indexPath.row] as String)
var objects: [EventScheduleOfc]
try objects = context.fetch(fetchRequest) as! [EventScheduleOfc]
eventSchOfc = objects[0] as EventScheduleOfc
break
default:break
}
}

catch {
print("Try Again")
}

eventDesc.setValue(eventSchOnc, forKey: "eventScheduleOnc")
eventDesc.setValue(eventSchOfc, forKey: "eventScheduleOfc")

let alert = UIAlertController(title: "title", message: "msg", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "yes", style: .default) { _ in
eventDesc.setValue(1, forKey: "eventStatus")
do {
try context.save()
} catch{
print("Try again")
}
})
alert.addAction(UIAlertAction(title: "no", style: .default) { _ in
eventDesc.setValue(2, forKey: "eventStatus")
do {
try context.save()
} catch {
print("Try again")
}
})
present(alert, animated: true, completion: nil)

let myEventCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! EventTableViewCell
let eventDescOnc = self.eventScheduleOnc[indexPath.row].eventDecOnc
let eventOnc = eventDescOnc?.eventStatus
if (eventOnc == 1) {
myEventCell.eventLabel.textColor = UIColor.green
}
else if (eventOnc == 2){
myEventCell.eventLabel.textColor = UIColor.red
}

let eventDescOfc = self.eventScheduleOfc[indexPath.row].eventDecOfc
let eventOfc = eventDescOfc?.eventStatus
if (eventOfc == 1) {
myEventCell.eventLabel.textColor = UIColor.green
}
else if (eventOfc == 2) {
myEventCell.eventLabel.textColor = UIColor.red
}

}


How do I display right text colors according to
eventStatus
under each
segment
?

Answer Source

Instead of using String array used array of EventScheduleOfc and EventScheduleONc and after you set eventStatus of EventDesc in didSelectRowAt simply reload the tableView.

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

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

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.eventScheduleOnc[indexPath.row].eventNameOnc

        if let eventDesc = self.eventScheduleOnc[indexPath.row].eventDec,           
           let eventStatus = eventDesc.eventStatus {
            if eventStatus == 1 {
                myEventCell.eventLabel.textColor = UIColor.green
            }
            else {
                myEventCell.eventLabel.textColor = UIColor.red
            }
        }
        else {
            myEventCell.eventLabel.textColor = UIColor.black
        }
    }
    else {
        myEventCell.eventLabel.text  = self.eventScheduleOfc[indexPath.row].eventNameOfc
        if let eventDesc = self.eventScheduleOfc[indexPath.row].eventDec,           
           let eventStatus = eventDesc.eventStatus {
            if eventStatus == 1 {
                myEventCell.eventLabel.textColor = UIColor.green
            }
            else {
                myEventCell.eventLabel.textColor = UIColor.red
            }
        }
        else {
            myEventCell.eventLabel.textColor = UIColor.black
        }
    }
    return myEventCell
}

Now in didSelect simply set the value in core data and then instead of setting textColor of label simply reload the tableView.