根据细分显示正确的文字颜色?

时间:2017-03-08 09:38:50

标签: swift xcode core-data uisegmentedcontrol

我有一个包含2个细分ONCOFC的细分控件,如果我在row1下更改了ONC的颜色,它会改变row1的颜色在OFC也是如此。我希望将文字颜色更改为green,如果是eventStatus = 1,则red更改为eventStatus = 2。这就是我到目前为止所做的事情

   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
    }

}

如何根据每个eventStatus下的segment显示正确的文字颜色?

1 个答案:

答案 0 :(得分:1)

而不是使用String数组使用EventScheduleOfcEventScheduleONc数组,并在eventStatus中设置EventDesc didSelectRowAt后,只需重新加载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
}

现在didSelect只需在核心数据中设置值,然后只需重新加载textColor,而不是设置标签的tableView