2017-04-17 3 views
-1

イベントを作成すると、それは黒色で、アクションコントローラからはいまたはいいえを選択すると、それに応じて緑色または赤色に変わります。私は行でイベントを選択し、yesを選択した場合グループ内のデータのソート

、それは​​になり、私はそれがredをオンにし、私はNo sectionの下でそれをソートしたいとno場合、Yes sectionの下でそれを並べ替えることが好きです。しかし、私が作成したすべてのイベントがEvents sectionの下にグループ化されているので、私はそれを行うことができません。彼らが所属するセクションの下でイベントを送信する方法を知りたい。

例えば、私はグループで私のイベントを並べ替えるのが好き、私はEventsの下に滞在するNoGeo 101 , Stats 101YesMath101English101を見たい.Iは、以下の私のコード内の異なる場所でOncYesOncNoを移入するために多くのことを試してみました無駄に数日間。

enter image description here

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

どのように私はグループによって、このデータをソートしますか?

+0

私はあなたの質問から何か意味をなさない、私は行っていないあなたが話していることを理解しようとするためにあなたのタラを歩き回る。 –

+0

@DuncanC、これが明らかな場合はお知らせください。 – Coder221

+0

Nope。イベントとは何ですか?定義を表示します。イベントはどのように整理されていますか?あなたのデータ構造のレイアウトとすべてが意味することを説明してください。 –

答えて

0

私は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 
     } 
    } 
関連する問題