【问题标题】:Sorting data in groups分组数据排序
【发布时间】:2017-04-17 01:27:44
【问题描述】:

这是我的界面的外观,如果我创建一个事件,它是黑色的,一旦我从动作控制器中选择是或否,它就会相应地变成绿色或红色。

如果我在行中选择一个事件并选择yes,它会变成green,我喜欢在Yes section下排序,如果no它变成red,我喜欢在@987654328下排序@。但我无法做到这一点,因为我创建的所有事件都归入Events section。我想知道如何在它们所属的部分下发送事件。

例如,我喜欢按组对事件进行排序,我喜欢在Yes 下看到English101No 下的Math101Geo 101 , Stats 101 留在Events 下。我尝试了很多在下面我的代码中的不同位置填充OncYesOncNo 几天都是徒劳的。

var eventTitleOnc:[String] = []
    var oncYes:[String] = []
    var oncNo:[String] = []
    var oncEventsPresent: [String] = []
    var eventType:[Int] = []
    var oncEvents : [String] = []
var eventScheduleOnc = [EventScheduleOnc]()
let sectionTitles:[String] = ["Events","Yes","No"]

  override func viewDidLoad() {
    super.viewDidLoad()
       eventDataOnc()
        for i in stride(from: 0, to: eventTitleOnc.count, by: 1){
                oncEventsPresent.append(eventTitleOnc[i])
            }
        myEventTableView.reloadData()
    }

// Number of Rows in Section

 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        var typeOfEvent = 0

        if(section == 0)
        {
         typeOfEvent = oncEventsPresent.count
        }
        if(section == 1)
        {
            typeOfEvent = oncYes.count
        }
        if(section == 2)
        {
            typeOfEvent = oncNo.count
        }
    return typeOfEvent
}

// Cell for Row
 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let myEventCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! EventTableViewCell
        myEventCell.eventLabel.text = self.eventScheduleOnc[indexPath.row].eventNameOnc
        let eventDesc = self.eventScheduleOnc[indexPath.row].eventDecOnc
        let eventStatus = eventDesc?.eventStatus
           if eventStatus == 1 {
                myEventCell.eventLabel.textColor = UIColor.green
            }
            else if eventStatus == 2{
                myEventCell.eventLabel.textColor = UIColor.red
            }
        else
           {
            myEventCell.eventLabel.textColor = UIColor.black
            }
          }
           return myEventCell
    }


//Did Select Row at index path

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context) as! EventDec
    var eventSchOnc :EventScheduleOnc?
    do {
            let fetchRequestOnc:NSFetchRequest<EventScheduleOnc> = EventScheduleOnc.fetchRequest()
            fetchRequestOnc.returnsObjectsAsFaults = false
             fetchRequestOnc.predicate = NSPredicate(format: "eventNameOnc == %@",self.oncEventsPresent[indexPath.row] as String )
             var eventSchOncObjects = [EventScheduleOnc]()
             try eventSchOncObjects = context.fetch(fetchRequestOnc)
             eventSchOnc = eventSchOncObjects[0] as EventScheduleOnc
            }
catch {}
    eventDesc.setValue(eventSchOnc, forKey: "eventScheduleOnc")
    let alert = UIAlertController(title: "title", message: "msg", preferredStyle: .alert)
    DispatchQueue.main.async {
    alert.addAction(UIAlertAction(title: "yes", style: .default ){ _ in
        eventDesc.setValue(1, forKey: "eventStatus")
        do {
            try context.save()
            self.myEventTableView.reloadData()
           }
        catch{}
    })
    alert.addAction(UIAlertAction(title: "no", style: .default) { _ in
        eventDesc.setValue(2, forKey: "eventStatus")

        do{
            try context.save()
            self.myEventTableView.reloadData()
          }
        catch {}
    })
  }
present(alert, animated: true, completion: nil)
}

//MARK: - Core data

private func eventDataOnc(){
    let eventsOncFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName:"EventScheduleOnc")
    let primarySortDescriptor = NSSortDescriptor(key:"eventDecOnc.eventStatus", ascending: false)
    let secondarySortDescriptor = NSSortDescriptor(key:"eventNameOnc", ascending: true)
    eventsOncFetchRequest.sortDescriptors = [primarySortDescriptor,secondarySortDescriptor]
do
    {
        let results = try context.fetch(eventsOncFetchRequest)
        if results.count > 0
        {
            for result in results
            {
                if let eventDisTextOnc = (result as AnyObject).value(forKey: "eventNameOnc") as? String
                {
                        eventTitleOnc.append(eventDisTextOnc)
                }
            }
        }
            self.eventScheduleOnc = try context.fetch(eventsOncFetchRequest) as! [EventScheduleOnc]
    }
    catch{print(error.localizedDescription)}
}

如何按组对这些数据进行排序?

【问题讨论】:

  • 我无法理解你的问题,而且我不会费尽心思去弄清楚你在说什么。
  • @DuncanC,如果清楚,请告诉我
  • 不。什么是事件?显示定义。您的活动是如何组织的?解释数据结构的布局以及所有内容的含义。

标签: ios swift xcode core-data


【解决方案1】:

我会使用一个包含 3 个部分的表格视图:

enum Section { 
     case undecided, yes, no 
     static let all: [Section] = [.undecided, .yes, .no]
}

然后我会将我的数据粘贴到一个数组字典中(或者一个带有关联值的枚举数组):

var events[Section: [String]] = [.undecided: [], .yes: [], .no: []]

您的数据源方法如下所示:

    extension V: UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return Sections.all.count
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return events[Sections.all[section]]!.count
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyCell.self), for: indexPath) as MyCell
            cell.event = events[Sections.all[section]]!
            return cell
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2011-08-22
    • 2021-10-31
    • 2021-11-24
    相关资源
    最近更新 更多