ios – 将按日期分隔的部分添加到Swift中的UITableView

前端之家收集整理的这篇文章主要介绍了ios – 将按日期分隔的部分添加到Swift中的UITableView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Swift和iOS编程的完整菜鸟,所以你必须原谅这个简单的问题.

我创建了一个tableView,它在按下一个按钮时显示数组(字符串)的内容.
现在,我想在tableView部分中按照日期排序这些字符串.

更详细地说:当用户点击按钮时,字符串应该插入到数组的索引0处,并显示在一个标题为日期的部分.如果数组中的值大于今天的值,那么这些值应显示在该日期的单独部分中.每个部分应对应24小时的一天,并显示当天添加的所有字符串.

以下是我迄今为止所取得的一些示例代码

  1. var testArray[String]()
  2. var sectionsInTable[String]()
  3.  
  4. @IBOutlet weak var testTable: UITableView!
  5.  
  6. @IBAction func saveButton(sender: AnyObject) {
  7.  
  8. testArray.insert("\(strTest)",atIndex: 0)
  9. testTable.reloaddata()
  10.  
  11. }
  12.  
  13. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  14.  
  15. return sectionsInTable.count
  16.  
  17. }
  18.  
  19. func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{
  20.  
  21. return testArray.count
  22.  
  23. }
  24.  
  25. func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  26.  
  27. var cell = UITableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: "Cell")
  28.  
  29. cell.textLabel.text = String(testArray[indexPath.row])
  30.  
  31. return cell
  32. }

我真的不知道如何管理这些部分.希望有人能指出我正确的方向.谢谢!

解决方法

我通常会用Core Data和 NSFetchedResultsController来完成,因为它有内置的方法获取部分.

但是,我不用使用Core Data来回答这个问题.代码有点乱七八糟,但我们走了

首先,您必须创建一个存储日期和文本的对象. testArray将是这些对象的数组,而不是String数组.例如:

  1. class DateTextItem: NSObject {
  2. var text: String = ""
  3. var insertDate: NSDate = NSDate()
  4. }
  5.  
  6. var testArray = [DateTextItem]()

然后当saveButton被命中时,我们将创建并添加DateTextItem对象.如果dateInTable不存在,我们还将添加日期.

  1. @IBAction func saveButton(sender: AnyObject) {
  2. let newItem = DateTextItem()
  3. newItem.text = "Test \(testArray.count)"
  4.  
  5. // this is for development only
  6. // increment the date after 2 records so we can test grouping by date
  7. if testArray.count >= (testArray.count/2) {
  8. let incrementDate = NSTimeInterval(86400*(testArray.count/2))
  9. newItem.insertDate = NSDate(timeIntervalSinceNow:incrementDate)
  10. }
  11.  
  12. testArray.append(newItem)
  13.  
  14. // this next bit will create a date string and check if it's in the sectionInTable
  15. let df = NSDateFormatter()
  16. df.dateFormat = "MM/dd/yyyy"
  17. let dateString = df.stringFromDate(newItem.insertDate)
  18.  
  19. // create sections NSSet so we can use 'containsObject'
  20. let sections: NSSet = NSSet(array: sectionsInTable)
  21.  
  22. // if sectionsInTable doesn't contain the dateString,then add it
  23. if !sections.containsObject(dateString) {
  24. sectionsInTable.append(dateString)
  25. }
  26.  
  27. self.tableView.reloadData()
  28. }

接下来,我创建了一个函数获取一个部分中的项目,因为我们需要它在几个地方.

  1. func getSectionItems(section: Int) -> [DateTextItem] {
  2. var sectionItems = [DateTextItem]()
  3.  
  4. // loop through the testArray to get the items for this sections's date
  5. for item in testArray {
  6. let dateTextItem = item as DateTextItem
  7. let df = NSDateFormatter()
  8. df.dateFormat = "MM/dd/yyyy"
  9. let dateString = df.stringFromDate(dateTextItem.insertDate)
  10.  
  11. // if the item's date equals the section's date then add it
  12. if dateString == sectionsInTable[section] as NSString {
  13. sectionItems.append(dateTextItem)
  14. }
  15. }
  16.  
  17. return sectionItems
  18. }

最后,这里是Table View数据源方法

  1. // MARK: - Table view data source
  2. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  3. return sectionsInTable.count
  4. }
  5.  
  6. override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
  7. return self.getSectionItems(section).count
  8. }
  9.  
  10. override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  11.  
  12. // Configure the cell...
  13. var cell = UITableViewCell(style: UITableViewCellStyle.Default,reuseIdentifier: "Cell")
  14.  
  15. // get the items in this section
  16. let sectionItems = self.getSectionItems(indexPath.section)
  17. // get the item for the row in this section
  18. let dateTextItem = sectionItems[indexPath.row]
  19.  
  20. cell.textLabel.text = dateTextItem.text
  21.  
  22. return cell
  23. }
  24.  
  25. // print the date as the section header title
  26. override func tableView(tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
  27. return sectionsInTable[section]
  28. }

猜你在找的iOS相关文章