根据选择显示正确的文字颜色?

时间:2017-03-06 05:50:15

标签: swift xcode core-data

我喜欢将文字颜色更改为green,如果是eventStatus = 1,则red更改为eventStatus = 2。我尝试了以下操作,如果every event greenlast selected改为eventStatus = 1 every event,我会将red更改为last selected eventStatus = 2。如何针对每个单独的事件根据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{}
    }

3 个答案:

答案 0 :(得分:1)

您需要创建EventSchedule数组,而不是创建两个数组,并使用cellForRowAt方法使用该数组。

var eventSchedule = [EventSchedule]()

现在在eventData方法中初始化了这个数组。

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{}
}

现在使用cellForRowAt方法使用此单个数组。

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
    }
}

答案 1 :(得分:0)

您循环浏览fetchRequest中的cellForRowAt,因此eventLabel的颜色将根据上一个eventStatus的{​​{1}}进行设置。您应该将result存储为全局变量。

<强>解决方案

声明用于存储fetchRequest个对象的全局数组:

EventDec

然后,在var events: [EventDec] = [] 中,将数据插入数组:

eventData()

最后,在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) } 中,设置与事件的cellForRowAtIndexPath对应的textColor

eventStatus

答案 2 :(得分:0)

没错。 indexPath.row与您的获取结果不匹配,因为它会因您的forEach函数在循环内有效地进行循环。 forEach循环的最后一个结果将是所有单元格的结果。解决方案可能是根据您的cell.tag为您的所有单元格生成eventStatus并应用cell.tag值中的颜色。