2016-03-21 3 views
1

辞書の配列の配列から約150個の要素をロードしています。テーブルビューにすべてのデータを取り込めますが、そのスクロールが遅いときはスクロールします。自分の関数の情報をコンソールに表示すると、スクロールするたびにすべてのデータが返されているように見えます。これは私がうまく読み込めない(非同期的に)か、私の機能を変更する必要がありますか?テーブルビューのスロー/スタッタスクロール

func querySections() -> [String] { 
     var sectionsArray = [String]() 
     for task in tasks { 
      let dueTimes = task.dueTime 
      sectionsArray.append(dueTimes) 
     } 
     let uniqueSectionsArray = Array(Set(sectionsArray.sort())) 
//  print(uniqueSectionsArray) 
     return uniqueSectionsArray 
    } 


    func queryDueTimes(section:Int) -> [Task] { 
     var sectionItems = [Task]() 
     for task in tasks { 
      let dueTimes = task.dueTime 
      if dueTimes == querySections()[section] { 
       sectionItems.append(task) 
      } 
     } 
     print(sectionItems) 
     return sectionItems 
    } 



    // MARK: - Table view data source 

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return querySections()[section] 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return querySections().count 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return queryDueTimes(section).count 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! TaskCell 

     // Configure the cell... 
     cell.selectionStyle = .None 
     let times = queryDueTimes(indexPath.section) 
     let task = times[indexPath.row] 
     cell.label.text = task.title 
     if task.done == true { 
      cell.checkBox.image = UIImage(named: "checkedbox") 
      cell.detailLabel.text = "Completed By: \(task.completedBy)" 
      } 
      else { 
      cell.checkBox.image = UIImage(named: "uncheckedbox") 
      cell.detailLabel.text = "" 
      } 

     cell.delegate = self 
     return cell 
    } 

基本的に、querySectionsで、Iは、各タスクのdueTimesの全てを反復だと、私は重複の全てを除外できるように集合のアレイにそれらを変更します。これは私のすべてのセクションを私に与えている。 queryDueTimesについては、タスクを繰り返してセクションにマッチングさせています。

私はviewDidLoadで関数を呼び出すことについて考えていましたが、それは機能しません(関数の外部からアクセスできる他の空の配列に渡そうとすると空の配列を与え続けます) viewDidLoadの(私が行う方法を知っている限り)tアクセスセクション(queryDueTimes

更新1: 間違いは私の目的です。私は、タスクは配列の配列であり、その配列がタスクの配列(各タスクのすべてのプロパティを持つ構造体)であると言いました。アプリケーションを読み込むと、バックエンドのすべてのタスクがローカルの配列に追加されます(「タスク」)。これを動作させるための配列を用意する必要がありますか、何とか私のコードを修正して動作させることはできますか?

アップデート2:私はそれらを印刷するとき、私はsectionTimestasksInSectionArrayとして空の配列を取得しています

var sectionTimes = [String]() 
    var tasksInSectionArray = [[Task]]() 
    var tasks = [Task]() { 
     didSet { 
      tableView?.reloadData() 
     } 
    } 

    func updateTableView() { 
      sectionTimes = Set(tasks.map{$0.dueTime}).sort() 
      tasksInSectionArray = sectionTimes.map{section in tasks.filter{$0.dueTime == section}} 
      print(sectionTimes) 
      print(tasksInSectionArray) 
      tableView.reloadData() 
     } 

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
      return sectionTimes[section] 
     } 

     override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return sectionTimes.count 
     } 

     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return tasksInSectionArray[section].count 
     } 


     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! TaskCell 

      // Configure the cell... 
      cell.selectionStyle = .None 
      let task = tasksInSectionArray[indexPath.section][indexPath.row] 
+0

推測しないでください。推測しないでください。楽器を使用!スクロール中に何が時間を費やしているかを正確に教えてくれます。それであなたはあなたの注意をどこに集中させるかを知っています。 – matt

+0

私はそれをどのように使用するかまだまだ実際にはわかりません。私はまだiOS用のSwiftで開発するのがとても新しいです。あなたは正しい方向に1つまたは2つを提供することで私を助けることができますか? –

+0

残念ながら、DManはそれを目の当たりにしてあなたの問題を解決しているようです。インストゥルメントを使用することを学ぶ方が良いと思うので、それは悪いことです! :))))))) – matt

答えて

1
var sections: [String] = [] 
var data: [[Tasks]] = [] 

func updateTableView() { 
    sections = Set(tasks.map { $0.dueTime }).sort() 
    data = sections.map { section in tasks.filter { $0.dueTime == section } } 
    tableView.reloadData() 
} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return sections.count 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return data[section].count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let task = data[indexPath.section][indexPath.row] 

    // Cell configuration 
} 

これは基本的にDManの言葉ですが、私はあなたのために例を挙げました。

+0

これはあなたが望むものと正確には異なる可能性があるので、自分の好みに合わせて並べ替える必要があります。しかし、一般的に、これはそれをスピードアップするはずです。 – Eendje

+0

申し訳ありません。私はまだこれで本当に新しいです。私が間違っていると私は間違って連絡したかもしれません。updateTableView()(data = ...)の2行目で[Task]をTaskのクロージャの結果タイプに変換できないというエラーが発生しました。 –

+0

ごめんなさい。誰もが間違いを犯すので、私は間違いをしたのですみません。私は答えを編集します。 – Eendje

3

推測したように、データは1回だけではなく、繰り返し読み込まれ、並べ替えられています。 querySelectionsqueryDueTimesの結果を保存し、それをテーブルビューのデータソースメソッド内で使用します。

viewDidLoadでこれを行うことができます。両方の関数を一度呼び出し、クラスレベルの変数に結果を代入してから、tableView.reloadData()(テーブルビューへの参照があると仮定します)を呼び出します。

+0

私にそれを打つ:D +1 –

+0

私はおなじみのバンプを打つので、私はこれを前に試したと思う。私がcellForRowに着くと、コードの最初の2行で問題が発生します。時間を許して仕事をさせてください。 sectionTimes [indexPath.section]に時間を割り当てることができます(sectionTimesはクラスレベルにあります)。しかし、タスクを[indexPath.row]に割り当てることはできません。 –