[英]Group and Sort TableView by Date
i want ask how to grouping data (section) by Date? 我想问如何按日期对数据(部分)进行分组?
there is my code : 有我的代码:
import UIKit
import CoreData
class TableViewController: UITableViewController {
var myList = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
let appDel : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context : NSManagedObjectContext = appDel.managedObjectContext!
let freq = NSFetchRequest(entityName: "Item")
myList = context.executeFetchRequest(freq, error: nil)!
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
var object : NSManagedObject = myList[indexPath.row] as! NSManagedObject
let name = object.valueForKeyPath("name") as! String
let qty = object.valueForKeyPath("qty") as! Int
let date = object.valueForKeyPath("date") as! String
cell.nameLabel.text = name
cell.qtyLabel.text = toString(qty)
cell.dateLabel.text = date
return cell
}
}
in detail : at now my date sample is just row (just 1 section default) example : 详细信息:现在,我的日期示例只是行(默认为1个部分)示例:
apple 1 Jul 10, 2015 (row 1)
orange 7 Jul 10, 2015 (row 2)
grape 5 Jul 11, 2015 (row 3)
i want grouping by date so this result like : 我想按日期分组,所以这样的结果:
Jul 11, 2015 (section 1)
grape 5 (row 1)
Jul 10, 2015 (section 2)
apple 1 (row 2)
orange 7 (row 3)
NB : section is dynamic, depends at data, and sorting date descending 注意:该部分是动态的,取决于数据,并且排序日期降序
anyone have solution? 有人有解决方案吗? thx before
在此之前
Your list, myList, should be a form of dictionary or array of array. 您的列表myList应该是字典或数组数组的形式。
for example, if it's a dictionary, it should be something like 例如,如果是字典,则应该是
var myList: [String: [NSManagedObject]] = [:]
which has date as key 以日期为关键
it's all about sorting your data into right format. 这就是将数据整理成正确格式的全部。 In viewDidAppear you can do something like
在viewDidAppear中,您可以执行以下操作
for object in freq {
if let array = myList[object.valueForKeyPath("date")!] {
var a = array
a.append(object)
myList[object.date!] = a
} else {
myList[object.date!] = [object]
}
}
then in numberOfSectionsInTableView, 然后在numberOfSectionsInTableView中,
return myList.count
in numberOfRowsInSection 在numberOfRowsInSection中
if let a = myList[sections[section]] {
return a.count
}
return 0
}
with sections sorted 与部分排序
var sections: [String] {
return Array(myList.keys).sorted(<)
}
in cellForRowAtIndexPath, you can get a corresponding object. 在cellForRowAtIndexPath中,您可以获得一个对应的对象。
let array = myList[sections[indexPath.section]]!
let object = array[indexPath.row]
Also you need to implement viewForHeaderInSection with 另外,您还需要使用以下方法实现viewForHeaderInSection
let date = sections[section]
I'm sure there is better way to do this but this is good enough to populate the table view dynamically. 我相信有更好的方法可以做到这一点,但这足以动态填充表视图。 I just want you to see an idea.
我只想让您看到一个主意。 Hope it helps you.
希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.