将由date分隔的部分添加到Swift中的UITableView

我在Swift和iOS编程方面是一个完整的新手,所以你不得不原谅这个简单的问题。

我创build了一个tableView,它在按下button时显示一个数组的内容(string)。 现在,我想在tableView部分按datesorting这些string。

更详细地说:当用户点击button时,string应该被插入在数组的索引0处,并且显示在具有今天date的标题的部分中。 如果数组中的值比今天的date早,那么应该在该date的单独部分中显示这些值。 每个部分应该对应一个24小时的一天,并显示在当天添加的所有string。

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

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 } 

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

我通常会使用Core Data和NSFetchedResultsController来做这件事,因为它有内置的获取部分的方法。

但是,我将回答这个问题,而不使用核心数据。 代码是有点混乱,但我们现在去…

首先,你必须创build一个对象来存储date和文本。 testArray将是这些对象的一个​​数组,而不是一个String数组。 例如:

 class DateTextItem: NSObject { var text: String = "" var insertDate: NSDate = NSDate() } var testArray = [DateTextItem]() 

然后,当saveButton被击中时,我们将创build并添加DateTextItem对象。 如果它还不存在,我们也将这个date添加到sectionsInTable中。

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

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

 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 } 

最后,这里是表视图数据源方法的样子

 // 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] } 

我需要类似的东西,虽然Ron Fessler的解决scheme是有效的,但是当有很多节/行时,表格加载数据需要很长时间,甚至在这之后,响应也不是很好。 主要问题,我认为是getSectionItems函数,因为它将始终通过所有的项目…

我的解决scheme

 struct TableItem { let title: String let creationDate: NSDate } var sections = Dictionary<String, Array<TableItem>>() var sortedSections = [String]() @IBAction func saveButton(sender: AnyObject) { let date:String = "your date in string..." //if we don't have section for particular date, create new one, otherwise we'll just add item to existing section if self.sections.indexForKey(date) == nil { self.sections[date] = [TableItem(title: name, creationDate: date)] } else { self.sections[date]!.append(TableItem(title: name, creationDate: date)) } //we are storing our sections in dictionary, so we need to sort it self.sortedSections = self.sections.keys.array.sorted(>) self.tableView.reloadData() } 

tableView的dataSource方法:

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return sections.count } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sections[sortedSections[section]]!.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("Cell") let tableSection = sections[sortedSections[indexPath.section]] let tableItem = tableSection![indexPath.row] cell.titleLabel?.text = tableItem.title return cell } override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sortedSections[section] } 

你必须为每一天制作一个数组(例如,称为dayArray []),并将其添加到sectionInTable []中,并执行如下操作:

 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return sectionsInTable.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ return sectionsInTable.objectAtIndex(section).count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") cell.textLabel.text = String(sectionInTable.objectAtIndex(indexPath.section).objectAtIndex(indexPath.row)) return cell } 

对不起,如果我犯了错误,我不熟悉迅速,但我认为这个想法可以帮助。