我有一个包含2个细分ONC
和OFC
的细分控件,如果我在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
显示正确的文字颜色?
答案 0 :(得分:1)
而不是使用String数组使用EventScheduleOfc
和EventScheduleONc
数组,并在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
。