我是
Swift和iOS编程的完整菜鸟,所以你必须原谅这个简单的问题.
我创建了一个tableView,它在按下一个按钮时显示数组(字符串)的内容.
现在,我想在tableView部分中按照日期排序这些字符串.
更详细地说:当用户点击按钮时,字符串应该插入到数组的索引0处,并显示在一个标题为日期的部分.如果数组中的值大于今天的值,那么这些值应显示在该日期的单独部分中.每个部分应对应24小时的一天,并显示当天添加的所有字符串.
以下是我迄今为止所取得的一些示例代码:
- var testArray[String]()
- var sectionsInTable[String]()
- @IBOutlet weak var testTable: UITableView!
- @IBAction func saveButton(sender: AnyObject) {
- testArray.insert("\(strTest)",atIndex: 0)
- testTable.reloaddata()
- }
- func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return sectionsInTable.count
- }
- func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{
- return testArray.count
- }
- func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- var cell = UITableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: "Cell")
- cell.textLabel.text = String(testArray[indexPath.row])
- return cell
- }
我真的不知道如何管理这些部分.希望有人能指出我正确的方向.谢谢!
解决方法
我通常会用Core Data和
NSFetchedResultsController来完成,因为它有内置的方法来获取部分.
但是,我不用使用Core Data来回答这个问题.代码有点乱七八糟,但我们走了
首先,您必须创建一个存储日期和文本的对象. testArray将是这些对象的数组,而不是String数组.例如:
- class DateTextItem: NSObject {
- var text: String = ""
- var insertDate: NSDate = NSDate()
- }
- var testArray = [DateTextItem]()
然后当saveButton被命中时,我们将创建并添加DateTextItem对象.如果dateInTable不存在,我们还将添加日期.
- @IBAction func saveButton(sender: AnyObject) {
- let newItem = DateTextItem()
- newItem.text = "Test \(testArray.count)"
- // this is for development only
- // increment the date after 2 records so we can test grouping by date
- if testArray.count >= (testArray.count/2) {
- let incrementDate = NSTimeInterval(86400*(testArray.count/2))
- newItem.insertDate = NSDate(timeIntervalSinceNow:incrementDate)
- }
- testArray.append(newItem)
- // this next bit will create a date string and check if it's in the sectionInTable
- let df = NSDateFormatter()
- df.dateFormat = "MM/dd/yyyy"
- let dateString = df.stringFromDate(newItem.insertDate)
- // create sections NSSet so we can use 'containsObject'
- let sections: NSSet = NSSet(array: sectionsInTable)
- // if sectionsInTable doesn't contain the dateString,then add it
- if !sections.containsObject(dateString) {
- sectionsInTable.append(dateString)
- }
- self.tableView.reloadData()
- }
接下来,我创建了一个函数来获取一个部分中的项目,因为我们需要它在几个地方.
- func getSectionItems(section: Int) -> [DateTextItem] {
- var sectionItems = [DateTextItem]()
- // loop through the testArray to get the items for this sections's date
- for item in testArray {
- let dateTextItem = item as DateTextItem
- let df = NSDateFormatter()
- df.dateFormat = "MM/dd/yyyy"
- let dateString = df.stringFromDate(dateTextItem.insertDate)
- // if the item's date equals the section's date then add it
- if dateString == sectionsInTable[section] as NSString {
- sectionItems.append(dateTextItem)
- }
- }
- return sectionItems
- }
最后,这里是Table View数据源方法
- // MARK: - Table view data source
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return sectionsInTable.count
- }
- override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
- return self.getSectionItems(section).count
- }
- override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- // Configure the cell...
- var cell = UITableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: "Cell")
- // get the items in this section
- let sectionItems = self.getSectionItems(indexPath.section)
- // get the item for the row in this section
- let dateTextItem = sectionItems[indexPath.row]
- cell.textLabel.text = dateTextItem.text
- return cell
- }
- // print the date as the section header title
- override func tableView(tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
- return sectionsInTable[section]
- }