max*_*ter 19 date uitableview ios sections swift
我是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
}
Run Code Online (Sandbox Code Playgroud)
我真的不知道如何管理部分.希望有人可以指出我正确的方向.谢谢!
Ada*_*don 25
我需要类似的东西,虽然Ron Fessler的解决方案有效,但是当有很多部分/行时,表需要很长时间才能加载数据,甚至在此之后也没有太多响应.我认为主要问题是getSectionItems函数,因为它总是会遍历所有项目...
我的解决方案
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()
}
Run Code Online (Sandbox Code Playgroud)
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]
}
Run Code Online (Sandbox Code Playgroud)
Ron*_*ler 21
我通常会使用Core Data和NSFetchedResultsController执行此操作,因为它具有获取节的内置方法.
但是,我将在不使用Core Data的情况下回答这个问题.代码有点乱,但我们走了......
首先,您必须创建一个将存储日期和文本的对象.testArray将是这些对象的数组,而不是String数组.例如:
class DateTextItem: NSObject {
var text: String = ""
var insertDate: NSDate = NSDate()
}
var testArray = [DateTextItem]()
Run Code Online (Sandbox Code Playgroud)
然后当命中saveButton时,我们将创建并添加DateTextItem对象.如果sectionsInTable尚不存在,我们还会将其添加到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()
}
Run Code Online (Sandbox Code Playgroud)
接下来,我创建了一个函数来获取一个部分中的项目,因为我们需要在几个地方.
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
}
Run Code Online (Sandbox Code Playgroud)
最后,这是表视图数据源方法的样子
// 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]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32959 次 |
| 最近记录: |